题目:

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. oop_day02_类、重载_20150810

    oop_day02_类.重载_20150810 1.怎样创建类?怎样创建对象? 2.引用类型之间画等号: 家门钥匙 1)指向同一个对象(数据有一份) 2)对当中一个引用的改动.会影响另外一个引用 基本 ...

  2. 学界| UC Berkeley提出新型分布式框架Ray:实时动态学习的开端—— AI 应用的系统需求:支持(a)异质、并行计算,(b)动态任务图,(c)高吞吐量和低延迟的调度,以及(d)透明的容错性。

    学界| UC Berkeley提出新型分布式框架Ray:实时动态学习的开端 from:https://baijia.baidu.com/s?id=1587367874517247282&wfr ...

  3. bzoj3110 [Zjoi2013]K大数查询——线段树套线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3110 外层权值线段树套内层区间线段树: 之所以外层权值内层区间,是因为区间线段树需要标记下传 ...

  4. 原生JS---8

    原生js学习笔记8——Ajax基础   什么是Ajax 不刷新页面的情况下从服务器获取.提交数据的一种数据交互方式. Ajax使用步骤 1.创建Ajax对象 var httpRequest = new ...

  5. xml转换成数组array

    直接上代码,成功转换 if($data){ //返回来的是xml格式需要转换成数组再提取值,用来做更新 $startnum = strpos($data,"<xml>" ...

  6. tp 3.1.3 动态切换模板问题

    if($this->isMobile()) { C('DEFAULT_THEME', 'mobile'); // 这里定义手机模板目录 C('TMPL_CACHE_PREFIX', 'm_'); ...

  7. 点开,看一段,你就会喜欢上学习pandas,你该这么学!No.3

    身为一个啰嗦的博主,还是要说点啥,如果你不想学,直接拖拽到最后,发现彩蛋,然后3秒 我呢,有个小目标,就是把技术类的文章,写的有趣 编程呢,最难的是什么? 我投票给入门 其实,把一个人带进编程的大门是 ...

  8. A - Team

    Problem description One day three best friends Petya, Vasya and Tonya decided to form a team and tak ...

  9. C#之密封类(详解)

    10.3  密封类与密封方法 如果所有的类都可以被继承,那么很容易导致继承的滥用,进而使类的层次结构体系变得十分复杂,这样使得开发人员对类的理解和使用变得十分困难,为了避免滥用继承,C#中提出了密封类 ...

  10. RabbitMQ 官方NET教程(三)【发布/订阅】

    上一篇博客中,我们实现了工作队列,并且我们的工作队列中的一个任务只会发给一个工作者,除非某个工作者未完成任务意外被杀死,会转发给另外的工作者.在这部分中,我们会做一些完全不同的事情 - 我们会向多个消 ...