题目:

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. Hypercall

    在Linux中.大家应该对syscall很的了解和熟悉,其是用户态进入内核态的一种途径或者说是一种方式.完毕了两个模式之间的切换:而在虚拟环境中,有没有一种类似于syscall这样的方式.可以从no ...

  2. 如何运行开源的React Native项目?

    如何运行开源的RN项目? 1.下载 2.解压 3.配置本地sdk位置 sdk.dir = D\:\\Android\\SDK 4.调整gradle版本 apply plugin: "com. ...

  3. 倒排列表压缩算法汇总——分区Elias-Fano编码貌似是最牛叉的啊!

    来看看倒排索引压缩.压缩是拿CPU换IO的最重要手段之一,不论索引是放在硬盘还是内存中.索引压缩的算法有几十种,跟文本压缩不同,索引压缩算法不仅仅需要考虑压缩率,更要考虑压缩和解压性能,否则会解压太慢 ...

  4. SVM中的线性分类器

    线性分类器: 首先给出一个非常非常简单的分类问题(线性可分),我们要用一条直线,将下图中黑色的点和白色的点分开,很显然,图上的这条直线就是我们要求的直线之一(可以有无数条这样的直线)     假如说, ...

  5. PCB MS SQL 行转列(动态拼SQL)

    一.原数据: SELECT inman,indate FROM [fp_db].[dbo].[ppezhpbb] WHERE indate > '2016-5-1' AND indate < ...

  6. 试用php的ping命令

    使用PHP自动PING IP,校检网络连接是否正常! <?php $server = 'ping kalvin.cn -n 1'; $last_line = exec($server, $arr ...

  7. 编写linux 命令行实用工具 shell命令

    今天我想以带着问题的方法学习新的技术. 问题1: 编写一个命令 语法: command  options  path  expressions

  8. 5.20rieds切换数据库

  9. 关于sizeof()、size()的有些问题

    #include<iostream>using namespace std; int main() { char a[] = "abcdefg"; string s = ...

  10. bootstrap表格样式

    一:表格基本格式 <table> <tr> <th>标题一</th> <th>标题二</th> </tr> < ...