Strategy Game

Title Description

Bob likes to play computer games, especially strategy game. But he often could not find a way to quickly played the game. Now he has a problem.

He wants to establish an ancient castle, the castle road form a tree. He is to be placed on the minimum number of tree nodes soldiers so that soldiers can beheld all the way.

Note that, when a soldier on a node, all the edges connected to the node can be a glancing.

You compile a program that, given a tree to help Bob to calculate the minimum he needs to place soldiers.

Input Format

The first line N, the number of nodes in the tree.

The second row to row 1 + N, with each line describing information of each node, as follows: the reference node I, k (k edges behind connected to node I).

Next number k, respectively, of another node of each edge numeral r1, r2, ..., rk.

For a n (0 <n <= 1500) of the tree nodes, the node numbers between 0 and n-1, appears once every edge in the input data only.

Output Format

The output file contains only a number, minimum number of soldiers is asked for.

For example, for the tree shown below:

       0
1
2      3

The answer is 1 (as long as a soldier at node 1).

Sample input and output

Input # 1
4
0 1 1
1 2 2 3
2 0
3 0
Output # 1
1
 
analysis:
Tree of maximum independent set problem template. . . F [i] [0] indicates the position of the minimum hold current, F [i] [1] indicates the minimum value of the discharge current position.
 
CODE:
 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 const int M=1505;
 8 struct node{
 9     int num;
10     int child[M];
11 }k[M];
12 int f[M][2],a[M],n,root;
13 void dp(int x){
14     f[x][1]=1;
15     f[x][0]=0;
16     if (k[x].num==0) return ;
17     for (int i=1;i<=k[x].num;i++){
18         dp(k[x].child[i]);
19         f[x][0]+=f[k[x].child[i]][1];
20         f[x][1]+=min(f[k[x].child[i]][0],f[k[x].child[i]][1]);
21     }
22 }
23 int main() {
24     cin>>n;
25     for (int i=1;i<=n;i++){
26         int x,y;
27         cin>>x;
28         cin>>k[x].num;
29         for (int j=1;j<=k[x].num;j++){
30             cin>>y;
31             k[x].child[j]=y;
32             a[y]=1;
33         }
34     }
35     while (a[root]) root++;
36     dp(root);
37     cout<<min(f[root][0],f[root][1])<<endl;
38     //system("pause");
39     return 0;
40 }

 

Guess you like

Origin www.cnblogs.com/kanchuang/p/11243503.html