---
title: "POJ 1694-An Old Stone Game"
author: "maybeiloveu"
date: "2009-11-21T18:57:34Z"
tags: ["J#"]
slug: "poj-1694-an-old-stone-game"
draft: false
lastmod: "2009-11-21T19:01:18Z"
id: 522238
url: "https://maybeiloveu.iteye.com/blog/522238"
---

[http://acm.pku.edu.cn/JudgeOnline/problem?id=1694](http://acm.pku.edu.cn/JudgeOnline/problem?id=1694 "题目")

    题目大意，给一棵树，不断在树的叶结点上放石头，若一个中间节点p的子结点上都放置了石头，那么拿掉所有子结点上的石头，在p上放置一颗。目标是在树根结点上放置石头。求最少需要多少颗石头。

    最开始凭直觉在想， 应该要让“消去”之后剩下来的石头尽可能多 。可最后还是没能按照这个思路想下去，主要原因在于没有把问题中的规则搞清楚：

      要想在p上放石头，必然要先在它的所有子树上放石头，需要“满足”所有的子树，那么需要石头数量最多的子树（设其需要x个石头）必然需要满足。因为通过“消去”需要石头最多的子树，有可能满足其余所有的子树。满足p所需要的石头的最小数量就是x。

      这样一来程序就比较好写了，递归实现即可，叶结点需要石头1个，中间结点递归计算完子树的需要量之后，降序排序，“满足”需要量最大的子树之后，依次判断剩余量是否足够“满足”其余子树即可。

```
#include <cstdio>
#include <algorithm>

struct node {
    int id, need, nchild;
    int child[200];
} tree[200];

bool cmp(const int &a, const int &b) {
    return tree[a].need > tree[b].need;
}

int get_need(int p) {
    if (tree[p].nchild == 0) {
        tree[p].need = 1;
        return 1;
    }
    for (int i = 0; i < tree[p].nchild; i++)
        tree[tree[p].child[i]].need = get_need(tree[p].child[i]);
        
    std::sort(tree[p].child, tree[p].child + tree[p].nchild, cmp);

    int total = tree[tree[p].child[0]].need, left = total - 1;
    for (int i = 1; i < tree[p].nchild; i++) {
        if (left >= tree[tree[p].child[i]].need)
            left--;
        else {
            total += tree[tree[p].child[i]].need - left; //增加这么多才能满足这一棵子树
            left = tree[tree[p].child[i]].need - 1; //更新剩余石头的数量
        }
    }
    return total;
}

int main() {
    int test, nnode;
    scanf("%d", &test);
    while (test--) {
        scanf("%d", &nnode);
        for (int i = 0; i < nnode; i++) {
            scanf("%d%d", &tree[i].id, &tree[i].nchild);
            --tree[i].id;
            for (int j = 0; j < tree[i].nchild; j++) {
                scanf("%d", &tree[i].child[j]);
                --tree[i].child[j];
            }
        }
        printf("%d\n", get_need(0));
    }
    
    return 0;
}
```