POJ 1694 An Old Stone Game
题目:
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
代码:
#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的更多相关文章
- poj 1694 An Old Stone Game 树形dp
//poj 1694 //sep9 #include <iostream> #include <algorithm> using namespace std; const in ...
- POJ 1694 An Old Stone Game【递归+排序】
链接: http://poj.org/problem?id=1694 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...
- POJ 1740 A New Stone Game(博弈)题解
题意:有n个石子堆,每一个都可以轮流做如下操作:选一个石堆,移除至少1个石子,然后可以把这堆石子随便拿几次,随便放到任意的其他石子数不为0的石子堆,也可以不拿.不能操作败. 思路:我们先来证明,如果某 ...
- POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5453 Accepted: 2989 ...
- 【POJ】A New Stone Game(博弈论)
http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...
- POJ 3922 A simple stone game
题目: E - A simple stone game Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d &am ...
- poj 1740 A New Stone Game(博弈)
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5338 Accepted: 2926 ...
- Something about 博弈(POJ 3922 A simple stone game)
先是题目,本来是第三次训练的题,在这特别提出来讲. 先是题目: E - A simple stone game Time Limit:1000MS Memory Limit:65536KB ...
- poj 1115 Lifting the Stone 计算多边形的中心
Lifting the Stone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- HDU3183 RMQ/贪心
A Magic Lamp Problem Description Kiki likes traveling. One day she finds a magic lamp, unfortunately ...
- Codeforces Round #332 (Div. 2)C. Day at the Beach 树状数组
C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortuna ...
- axis2的wsdl无法使用eclipse axis1插件来生成client--解决方法
使用jetty+axis2实现webservice服务端,且无需使用axis2命令生成服务端代码.仅仅要services.xml配置实现类. project为gradleproject配置文件在src ...
- luogu1417 烹调方案
题目大意 一共有$n$件食材,每件食材有三个属性,$a_i$,$b_i$和$c_i$,如果在$t$时刻完成第$i$样食材则得到$a_i-t*b_i$的美味指数,用第$i$件食材做饭要花去$c_i$的时 ...
- 微信小程序初探(一、简单的数据请求)
微信小程序出来有一段时间了,之前没看好小程序(觉得小程序体验不咋好,内心对新事物有抵触心里,请原谅我的肤浅[捂脸][捂脸]),不过后来偶然之间玩过小程序的游戏(跳一跳.球球大作战.猜画小歌 等),顿悟 ...
- Ruby类扩张(extension)
创建: 2017/09/07 更新: 2017/09/16 修改标题字母大小写 ruby ---> Ruby 扩张类 class 类名 扩张的内容 end ...
- E20170830-mk
translation n. 翻译; 译本; 转化; 转变; calculate vt. 计算; 估计; 打算,计划; 旨在; erase vt. 抹去; 清除; 擦掉;
- ACM_来自不给标题的菜鸟出题组(巴什博弈+素数判定)
来自不给标题的菜鸟出题组 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大B和小b合作出一道程序设计月赛的题,他们的想法是给定一 ...
- xhtml1-strict.dtd
<!-- Extensible HTML version 1.0 Strict DTD This is the same as HTML 4 Strict except for changes ...
- HBase、Hive、MapReduce、Hadoop、Spark 开发环境搭建后的一些步骤(export导出jar包方式 或 Ant 方式)
步骤一 若是,不会HBase开发环境搭建的博文们,见我下面的这篇博客. HBase 开发环境搭建(Eclipse\MyEclipse + Maven) 步骤一里的,需要补充的.如下: 在项目名,右键, ...