UVA 1292-Strategic game

题目大意:给出一棵树,在某个选择某个结点可以覆盖和它相连的所有边,问最少选多少个结点所有边都被覆盖。

解题思路:首先将无根树转化为有根树,0为根。

用d[i][0]表示不选择结点i时覆盖以结点i为根的子树最少要多少个结点,用d[i][1]表示选择结点i时覆盖以结点i为根的子树最少要多少个结点。若结点i不选,为了和覆盖所有和结点i相连的结点,则每个儿子都必须选,若结点i选,则每个儿子选择较小的那个值。按DFS顺序递推。

状态转移方程:

d[i][0]=sum { d[u][1] }(u是i的儿子)

d[i][1]=sum { min { d[u][0],d[u][1] } }+1(u是i的儿子)

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

vector <int> node[1550];
int n, DP[1550][2], vis[1550];

void init() {
    for (int i = 0; i <= n; i++) {
        node[i].clear();
        DP[i][0] = DP[i][1] = 0;
        vis[i] = 0;
    }

    int x, y, num;
    for (int i = 0; i < n; i++) {
        scanf("%d:(%d)", &x, &num);
        for (int j = 0; j < num; j++) {
            scanf("%d", &y);
            node[x].push_back(y);
            node[y].push_back(x);
        }
    }
}

void DPS(int root) {
    vis[root] = 1;
    int num = node[root].size();
    for (int i = 0 ; i < num; i++) {
        int child = node[root][i];
        if (vis[child])
            continue;
        DPS(child);
        DP[root][0] += DP[child][1];
        DP[root][1] += min(DP[child][0], DP[child][1]);
    }
    DP[root][1]++;
}

int main() {
    while (scanf("%d", &n) != EOF) {
        init();
        DPS(0);
        printf("%d\n", min(DP[0][0], DP[0][1]));
    }
    return 0;
}

时间: 2024-10-27 18:00:38

UVA 1292-Strategic game的相关文章

UVA 1292 十二 Strategic game

Strategic game Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1292 Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is

Strategic game UVA - 1292

1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<vector> 6 #define mem(array) memset(array,0,sizeof(array)) 7 using namespace std; 8 9 int n,dp[1600][2]; 10 bool vis[1600]; 11 vecto

UVa 2038 - Strategic game(二分图最小顶点覆盖 or 树形DP)

Strategic game Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of wh

UVa 497 - Strategic Defense Initiative

题目:最大上升子序列,输出一组解. 分析:dp,LIS.数据较小 O(n^2)算法即可. 设以第i个数字作为最大上升子序列中的最后一个数的长度为 f(i),则有转移方程: f(i)= max(f(j)) { 0=< j < i  && data[j] < data[i] }: 用一个数组记录前驱,递归输出即可. 说明:注意输出格式有点纠结. #include <iostream> #include <cstdlib> #include <cs

uva 1292

题目大意 给定一棵树,选择尽量少的节点,使得每个没有选中的结点至少和一个已选结点相邻. 思路 经典的树形dp题,据说是最小顶点覆盖. f[u][0]: 表示不选i点,覆盖这个子树的最少点f[u][1]:选i点,覆盖这个子树的最少点 对于u点,如果选择这个点,那么他的字节点可选也可不选如果不选u点的话,那么它的子结点就必须要选!开始时我以为字节点只要至少选一个就可以了,但是这样是错的! 因为会出现下面这种情况: 顶点1不选,子节点中2有选了,但是3却没有相邻结点有选. 所以可以得到状态转移方程:

uva 1292(树形dp)

题意:有一个树,上面有n个结点,给出每个结点有边相连的直接相邻的点,问最少选几个点能让所有的边至少有一个结点被选中. 题解:树形dp简单题,把0当做根节点. f[i][0]:不选i点,覆盖所有边的最少点 f[i][1]:选i点,覆盖所有边的最少点 状态转移方程: f[u][1] += min(f[v][1], f[v][0]);//v是u的子节点,选u点子节点可以选或不选 f[u][0] += f[v][1];//不选u点就一定要选子节点,这样保证边能覆盖 #include <stdio.h>

[SinGuLaRiTy] 贪心题目复习

[SinGuLaRiTy-1024] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [POJ 2709] 颜料 (Painter) 题目描述 杂货店出售一种由N(3<=N<=12)种不同颜色的颜料,每种一瓶(50ML),组成的颜料套装.你现在需要使用这N种颜料:不但如此,你还需要一定数量的灰色颜料.杂货店从来不出售灰色颜料——也就是它不属于这N种之一.幸运的是,灰色颜料是比较好配置的,如果你取出三种不同颜色的颜料各x ml,混合起来就

UVA之1330 - City Game

[题目] Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees, factories and buildings. There is still some space in the area tha

UVA - 11609 Teams (排列组合数公式)

In a galaxy far far awaythere is an ancient game played among the planets. The specialty of the game isthat there is no limitation on the number of players in each team, as long asthere is a captain in the team. (The game is totally strategic, so som

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d