HDU 1054 Strategic Game

发布时间:2014-10-22 18:39:55编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"HDU 1054 Strategic Game",主要涉及到HDU 1054 Strategic Game方面的内容,对于HDU 1054 Strategic Game感兴趣的同学可以参考一下。

原题: Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4400    Accepted Submission(s): 1955 Problem 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 which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him? Your program should find the minimum number of soldiers that Bob has to put for a given tree. The input file contains several data sets in text format. Each data set represents a tree with the following description: the number of nodes the description of each node in the following format node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier or node_identifier:(0) The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data. For example for the tree:    the solution is one soldier ( at the node 1). The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table:   Sample Input 4 0:(1) 1 1:(2) 2 3 2:(0) 3:(0) 5 3:(3) 1 4 2 1:(1) 0 2:(0) 0:(0) 4:(0)   Sample Output 1 2   Source Southeastern Europe 2000   Recommend JGShining   题意+题解 给一棵树,树上的每一条边的两个端点上至少有一个端点被选,求选择的最小点集。 这个题跟刚才那个 HDU 2412 几乎是一样的,状态和相应的转移也非常好想 状态:dp[i][1/0] 表示选或不选第i个结点,在以第i个结点为根的子树中,选的最少的点,保证子树中每条边都至少有一个端点被选 转移:dp[i][1] += min(dp[son][0],dp[son][1]); dp[i][0] += dp[son][1];      //父亲不选,儿子结点必须选 代码: #include<cstdio> #include<vector> #include<algorithm> using namespace std; vector <int >T[1520]; int dp[1520][2],flag[1520]; int find_min(int a,int b) { return a<b?a:b; } void dfs(int u) { flag[u]=1; dp[u][1]=1; if (T[u].empty())return ; int i,k=T[u].size(),son; for ( i = 0; i < k; i++) { son=T[u][i]; if (!flag[son]) dfs(son); dp[u][0]+=dp[son][1]; dp[u][1]+= find_min(dp[son][1],dp[son][0]); } return ; } int main() { int n,i,root,num,j,son,source; while (scanf("%d",&n)!=EOF) { memset(dp,0,sizeof(dp)); memset(flag,0,sizeof(flag)); for ( i = 0; i < n; i++) { T[i].clear(); } for ( i = 0; i <n ; i++) { scanf("%d:(%d)",&root,&num); if(i==0)source=root; for ( j = 0; j <num ; j++) { scanf("%d",&son); T[root].push_back(son); } } dfs(source); printf("%d\n",find_min(dp[source][1],dp[source][0])); } return 0; }

上一篇:树形DP(3) Hdu3593 The most powerful force (泛化背包)