hdu1054

Strategic Game

Time Limit: 10000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 1054
64-bit integer IO format: %lld      Java class name: Main

Prev

Submit Status Statistics Discuss

Next

窗体顶端

Type:

None

 

Tag it!

窗体底端

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

题意:

       给出一棵n个结点的树,现在需要选择最少的点,使得所有的边都具有至少一个被选择的点,输出这个最少的点数。

分析:

       由于题目中的结点呈现树结构,所以可以考虑记忆化搜索。在建图的时候考虑存储每个结点子结点的数量。搜索点的顺序在树上是由上至下,dp[pos][0],表示当前位于pos结点并且不选择pos结点时这个pos结点对应的子树最少需要选择多少个点才能在子树上满足题目的条件;dp[pos][1]表示的则是选择pos这个结点。如果pos这个结点没有选择,则它的子结点必须全部都被选择;如果pos没有被选择,则它的子结点可选择可不选择。即dp[i][0]=sum(dp[son[i][j]][1]) dp[i][1]=sum(min(dp[son[i][j]][0],dp[son[i][j]][1]))

 1 #include <cstdio>
 2 #include <cstring>
 3 int min(int a,int b){return a < b ? a : b;}
 4 const int MAX_N = 1500;
 5 int n;
 6 int son[MAX_N + 1][MAX_N + 1],dp[MAX_N + 1][2],son_num[MAX_N + 1],f[MAX_N + 1];
 7 // 位于pos,放或者不放soldier的行为带来的数量答案
 8 int dfs(int pos,int key){
 9     if(dp[pos][key] != -1) return dp[pos][key];
10     int ans = key;
11     for(int i = 0 ; i < son_num[pos] ; i++)
12         if(key == 0)
13             ans += dfs(son[pos][i],1); // 子结点必须全部选择放1个soldier
14         else
15             ans += min(dfs(son[pos][i],0),dfs(son[pos][i],1));
16     return dp[pos][key] = ans;
17 }
18 int main(){
19     while(scanf("%d",&n) == 1){
20         memset(dp,-1,sizeof dp);
21         for(int i = 0 ; i < n ; i++) f[i] = i; // 用于寻找根结点
22         for(int i = 0 ; i < n ; i++){
23             int x,m; scanf("%d:(%d)",&x,&m);
24             son_num[x] = m;
25             for(int j = 0 ; j < m ; j++){
26                 scanf("%d",&son[x][j]);
27                 f[son[x][j]] = x;
28             }
29         }
30         int ans = 0;
31         for(int i = 0 ; i < n ; i++)if(f[i] == i)
32             ans = min(dfs(i,1),dfs(i,0));
33         printf("%d
",ans);
34     }
35     return 0;
36 }
View Code
原文地址:https://www.cnblogs.com/cyb123456/p/5917714.html