2200: An Old Stone Game
总提交: 29 测试通过:12
描述
There is an old stone game, played on an arbitrary general tree T. The goal is to put one stone on the root of T observing the following rules:
1. At the beginning of the game, the player picks K stones and puts them all in one bucket.
2. At each step of the game, the player can pick one stone from the bucket and put it on any empty leaf.
3. When all of the r immediate children of a node p each has one stone, the player may remove all of these r stones, and put one of the stones on p. The other r -1 stones are put back into the bucket, and can be used in the later steps of the game.
The player wins the game if by following the above rules, he succeeds to put one stone on the root of the tree.
You are to write a program to determine the least number of stones to be picked at the beginning of the game (K), so that the player can win the game on the given input tree.
输入
The input file describes several trees. The first line of this file is M, the number of trees (1 ≤ M ≤ 10). Description of these M trees comes next in the file. Each tree has N < 200 nodes, labeled 1, 2, ... N, and each node can have any possible number of children. Root has label 1. Description of each tree starts with N in a separate line. The following N lines describe the children of all nodes in order of their labels. Each line starts with a number p (1 ≤ p ≤ N, the label of one of the nodes), r the number of the immediate children of p, and then the labels of these r children.
输出
One line for each input tree showing the minimum number of stones to be picked in step 1 above, in order to win the game on that input tree.
样例输入
2
7
1 2 2 3
2 2 5 4
3 2 6 7
4 0
5 0
6 0
7 0
12
1 3 2 3 4
2 0
3 2 5 6
4 3 7 8 9
5 3 10 11 12
6 0
7 0
8 0
9 0
10 0
11 0
12 0
样例输出
3
4
题目来源
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n,m,t; 5 const int maxn=205; 6 vector<int> G[maxn]; 7 int dp[maxn]; 8 9 int dfs(int ee){ 10 int len=G[ee].size(); 11 if(len==0){ 12 dp[ee]=1; 13 return 1; 14 } 15 vector<int> vec; 16 for(int x:G[ee]){ 17 vec.push_back(dfs(x)); 18 } 19 sort(vec.begin(),vec.end()); 20 reverse(vec.begin(),vec.end()); 21 22 int res=vec[0]; 23 len=vec.size(); 24 for(int i=1;i<len;i++){ 25 res=res+vec[i]-min(res-i,vec[i]); 26 } 27 return res; 28 } 29 30 int main(){ 31 ios::sync_with_stdio(false); 32 while(cin>>n){ 33 while(n--){ 34 cin>>m; 35 for(int i=1,d1;i<=m;i++){ 36 cin>>d1>>t; 37 for(int j=1,d2;j<=t;j++){ 38 cin>>d2; 39 G[d1].push_back(d2); 40 } 41 } 42 43 cout << dfs(1) << endl; 44 for(int i=1;i<maxn;i++) G[i].clear(); 45 memset(dp,0,sizeof(dp)); 46 } 47 48 } 49 return 0; 50 }