HDU 1054 Strategic Game(树形DP)
Posted -凡-尘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1054 Strategic Game(树形DP)相关的知识,希望对你有一定的参考价值。
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2925 Accepted Submission(s): 1222
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:
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
#include <iostream> #include<cstdio> #include<algorithm> using namespace std; struct node { int brother,child; int yes;//该点放士兵 int no; //该点不放士兵 }tree[1505]; int n,i,j,k,root,num,origin; void dfs(int root) { int child=tree[root].child; while(child>0) { dfs(child); tree[root].yes+=min(tree[child].yes,tree[child].no); //父亲结点放置了,儿子结点可以放置也可以不放置 tree[root].no+=tree[child].yes; //父亲结点没有放置,儿子结点必须放置 child=tree[child].brother; } } int main() { while(~scanf("%d",&n)) { for(i=1;i<=n;i++) { tree[i].brother=tree[i].child=0; tree[i].yes=1; tree[i].no=0; } for(int t=1;t<=n;t++) { scanf("%d:(%d)",&root,&num); root++; if (t==1) origin=root; for(i=1;i<=num;i++) { int x; scanf("%d",&x); x++; tree[x].brother=tree[root].child; tree[root].child=x; } } dfs(origin); printf("%d\n",min(tree[origin].yes,tree[origin].no)); } return 0; }
Source
以上是关于HDU 1054 Strategic Game(树形DP)的主要内容,如果未能解决你的问题,请参考以下文章