题目:

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

详情见:http://blog.acmore.net/

代码:

 #include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int data[][];
int cmp(int a,int b)
{
return a > b;
}
int solve(int n)
{
if(data[n][] == )
return ;
int tmp[];
for(int i = ; i <= data[n][];i++)
tmp[i] = solve(data[n][i]);//递归求解
sort(tmp + ,tmp + data[n][] + ,cmp);
int ans = tmp[];
for(int i = ; i <= data[n][];i++)
if(tmp[i] > ans - i + )//此时数量少了,ans+1
ans++;
return ans;
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
for(int i = ; i < m; i++)
{
scanf("%d",&n);
scanf("%d",&data[n][]);
for(int j = ; j <= data[n][]; j++)
scanf("%d",&data[n][j]);
}
printf("%d\n",solve());
}
return ;
}

题意是:有一棵树,只有它的叶子上都放满石头,节点才能放一个石头,从叶子上拿下来的石头还可以继续使用,问:把根节点放上石头,最少需要多少石头?

因此要从最外面的叶子开始放。

代码虽然是从根节点开始求的,但是因为是递归,所以,最先求的还是最外层的叶子上的石头。

POJ 1694 An Old Stone Game的更多相关文章

  1. poj 1694 An Old Stone Game 树形dp

    //poj 1694 //sep9 #include <iostream> #include <algorithm> using namespace std; const in ...

  2. POJ 1694 An Old Stone Game【递归+排序】

    链接: http://poj.org/problem?id=1694 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  3. POJ 1740 A New Stone Game(博弈)题解

    题意:有n个石子堆,每一个都可以轮流做如下操作:选一个石堆,移除至少1个石子,然后可以把这堆石子随便拿几次,随便放到任意的其他石子数不为0的石子堆,也可以不拿.不能操作败. 思路:我们先来证明,如果某 ...

  4. POJ 1740 A New Stone Game

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5453   Accepted: 2989 ...

  5. 【POJ】A New Stone Game(博弈论)

    http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...

  6. POJ 3922 A simple stone game

    题目: E - A simple stone game Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d &am ...

  7. poj 1740 A New Stone Game(博弈)

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5338   Accepted: 2926 ...

  8. Something about 博弈(POJ 3922 A simple stone game)

    先是题目,本来是第三次训练的题,在这特别提出来讲. 先是题目: E - A simple stone game Time Limit:1000MS     Memory Limit:65536KB   ...

  9. poj 1115 Lifting the Stone 计算多边形的中心

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. 蒟蒻的数位DP专题总结

    BZOJ  1026: [SCOI2009]windy数: 题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1026           d ...

  2. bzoj 1800 & 洛谷 P2165 [AHOI2009]飞行棋 —— 模拟

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1800   https://www.luogu.org/problemnew/show/P21 ...

  3. Java压缩技术(二) ZIP压缩——Java原生实现

    原文:http://snowolf.iteye.com/blog/642298 去年整理了一篇ZLib算法Java实现(Java压缩技术(一) ZLib),一直惦记却没时间补充.今天得空,整理一下ZI ...

  4. nodejs安装express

    最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了, 书上命令为: 1 express -t ejs microblog 可是执行后 ...

  5. 解决emlog默认导航不能修改的问题以及修改后台登录地址的方法

    修改默认导航 我们需要使用修改代码的方式来解决 首先找到admin/navbar.php文件 然后找到需要删除的几行代码: if ($type != Navi_Model::navitype_cust ...

  6. 附加MySQL数据库的方法

    下面讲解附加MySQL数据库的方法. (1)将“光盘\Code\04\Project\数据库”文件夹中的扩展名为.sql的文件拷贝到本地机器中. (2)在如图1所示的MySQL工作台界面中,单击Dat ...

  7. Linux通信之poll机制分析

    poll机制分析 韦东山 2009.12.10 所有的系统调用,基于都可以在它的名字前加上“sys_”前缀,这就是它在内核中对应的函数.比如系统调用open.read.write.poll,与之对应的 ...

  8. JWPL工具处理维基百科wikipedia数据用于NLP

    JWPL处理维基百科数据用于NLP 处理zhwiki JWPL是一个Wikipedia处理工具,主要功能是将Wikipedia dump的文件经过处理.优化导入mysql数据库,用于NLP过程.以下以 ...

  9. 【原创】redhat5安装oracle10g

    安装缺失的包: 用 root 用户身份运行以下命令: rpm -q gcc make binutils openmotif setarch compat-db compat-gcc compat-gc ...

  10. 黑客常用dos命令

    http://blog.csdn.net/CSDN___LYY/article/details/77802438