An Old Stone Game(poj1694模拟与排序)

/*
http://poj.org/problem?id=1694
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#problem/E
An Old Stone Game
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3136 Accepted: 1424
Description


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: 
At the beginning of the game, the player picks K stones and puts them all in one bucket. 
At each step of the game, the player can pick one stone from the bucket and put it on any empty leaf. 
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. 
Input


The input 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.
Output


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.
Sample Input


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
Sample Output


3
4
Source


Tehran 1999
解析:
题意:从一棵树的叶子结点开始放石头。当且仅当儿子结点全部都放了石头之后,把儿子结点上的的石头都收了,往父节点放,以此类推。问最少需要准备多少石头最终可以在根节点上范石头
思路:来自http://blog.acmore.net/
Memory Time Language Length Submit Time
336 KB 0 ms C++ 756 B 2013-07-29 19:18:38

*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=210;
int tr[maxn][maxn];
bool cmp(int a,int b)
{
	return a>b;
}
int solve(int a)
{
	if(tr[a][0]==0)
	return 1;
	int sonum[maxn];//这个必须定义在该函数里面
	for(int i=1;i<=tr[a][0];i++)
	sonum[i]=solve(tr[a][i]);//每次都得更新值
	sort(sonum+1,sonum+1+tr[a][0],cmp);
	int ans=sonum[1];
	for(int i=2;i<=tr[a][0];i++)
	{
		if(sonum[i]>ans-i+1)
		ans++;
	}
	return ans;
}
int main()
{
	int i,j,n,m;
	int a,b,c;
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d",&n);
		memset(tr,0,sizeof(tr));
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&a,&b);
			tr[a][0]=b;
            for(j=1;j<=b;j++)
            {scanf("%d",&tr[a][j]);
            }
		}
		printf("%d\n",solve(1));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值