Strategic game
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 6194 | Accepted: 2853 |
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.
For example for the tree:
the solution is one soldier ( at the node 1).
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
For example for the tree:
![](http://poj.org/images/1463_1.jpg)
the solution is one soldier ( at the node 1).
Input
The input contains several data sets in text format. Each data set represents a tree with the following description:
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.
- the number of nodes
- the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.
Output
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:
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
分析:用一维数组表示树
dp[i][0]、dp[i][1]分别代表不在节点i上 和 在节点i上放置士兵时整个以i节点为根节点的子树被覆盖需要的最少士兵数量
dp[i][0]、dp[i][1]分别代表不在节点i上 和 在节点i上放置士兵时整个以i节点为根节点的子树被覆盖需要的最少士兵数量
状态转移:
对叶子节点>>>dp[i][0] = 0, dp[i][1] = 1; 即递归的出口(递归结束的情况,其实叶子节点是最先被赋值的,其实就是初始化!)
对非叶子节点>>>dp[i][0] = dp[j_1][0] + dp[j_2][0] + dp[j_3][0] + ... + dp[j_n][0] (j_1, j_2,...j_n为i的子节点) (子节点放了士兵,那么i就不用士兵了)
dp[i][1] = ∑min(dp[j][0], dp[j][1]) + 1;(j为i的子节点)(i放了士兵,i的子节点可以放或不放,所以求小者 求和)
其实还是小复杂的! 自己想怎么想出来啊 : ( 好好研究研究。。
//Strategic game——树形DP
//用一维数组表示树
//dp[i][0]、dp[i][1]分别代表不在节点i上和 在节点i上放置士兵时整个以i节点为根节点的子树被覆盖需要的最少数量
#include<iostream>
using namespace std;
int pre[1501], childCnt[1501], root, n;
int dp[1501][2];
//递归~
void DP(int root)
{
int dp0 = 0, dp1 = 0;
if(childCnt == 0)
{
dp[root][0] = 0;
dp[root][1] = 1;
return;
}
for(int i=0;i<n;i++)
{
if(pre[i] == root) // root的子节点i
{
DP(i);
dp1 += min(dp[i][1], dp[i][0]);
dp0 += dp[i][1];
}
}
dp[root][0] = dp0;
dp[root][1] = dp1 + 1;
}
int main()
{
while(scanf("%d",&n) != EOF)
{
int father, m, child;
memset(dp, 0, sizeof(dp));
memset(pre, -1, sizeof(pre));
memset(childCnt, -1, sizeof(childCnt));
root = -1;
for(int i=0;i<n;i++)
{
scanf("%d:(%d)",&father,&m); // OMG!
childCnt[father] = m; // 子节点的个数
if(root == -1) root = father; // 首先输入的是根节点
while(m--)
{
scanf("%d",&child);
pre[child] = father; // 父节点
if(child == root) root = father;
}
}
DP(root);
printf("%d\n",min(dp[root][0], dp[root][1]));
}
return 0;
}