zoukankan      html  css  js  c++  java
  • 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

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

    代码:

     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<string.h>
     4 #include<algorithm>
     5 using namespace std;
     6 int data[205][205];
     7 int cmp(int a,int b)
     8 {
     9     return a > b;
    10 }
    11 int solve(int n)
    12 {
    13     if(data[n][0] == 0)
    14         return 1;
    15     int tmp[205];
    16     for(int i = 1; i <= data[n][0];i++)
    17         tmp[i] = solve(data[n][i]);//递归求解
    18     sort(tmp + 1,tmp + data[n][0] + 1,cmp);
    19     int ans = tmp[1];
    20     for(int i = 2; i <= data[n][0];i++)
    21         if(tmp[i] > ans - i + 1)//此时数量少了,ans+1
    22             ans++;
    23     return ans;
    24 }
    25 int main()
    26 {
    27     int t,n,m;
    28     scanf("%d",&t);
    29     while(t--)
    30     {
    31         scanf("%d",&m);
    32         for(int i = 0; i < m; i++)
    33         {
    34             scanf("%d",&n);
    35             scanf("%d",&data[n][0]);
    36             for(int j = 1; j <= data[n][0]; j++)
    37                 scanf("%d",&data[n][j]);
    38         }
    39         printf("%d
    ",solve(1));
    40     }
    41     return 0;
    42 }

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

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

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

  • 相关阅读:
    Leetcode———重建二叉树
    springboot+quartz实现定时任务发送邮件demo
    MySQL备份,使用xtrabackup备份全实例数据时,会造成锁等待吗?那么如果使用mysqldump进行备份呢?
    MySQL高可用架构应该考虑什么?你认为应该如何设计?
    你为什么会决定进行分库分表,分库分表过程中遇到什么难题,如何解决的?
    MySQL主从复制什么原因会造成不一致,如何预防及解决?
    用什么方法可以防止误删数据?
    MySQL每天产生了多大容量的binlog,用SQL语句能查到吗?
    你遇到过哪些原因造成MySQL异步复制延迟?
    为什么说 pt-osc 可能会引起主从延迟,有什么好办法解决或规避吗?
  • 原文地址:https://www.cnblogs.com/riddle/p/3224083.html
Copyright © 2011-2022 走看看