Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320656
  • 博文数量: 179
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2083
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2024年(1)

2019年(13)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类:

2009-06-15 12:54:24

Marbles on a tree

Time Limit:1sMemory limit:32M
Accepted Submit:64Total Submit:99

n boxes are placed on the vertices of a rooted tree, which are numbered from 1 to n, 1 ≤ n ≤ 10000. Each box is either empty or contains a number of marbles; the total number of marbles is n. The task is to move the marbles such that each box contains exactly one marble. This is to be accomplished be a sequence of moves; each move consists of moving one marble to a box at an adjacent vertex. What is the minimum number of moves required to achieve the goal?

Input

The input contains a number of cases. Each case starts with the number n followed by n lines. Each line contains at least three numbers which are: v the number of a vertex, followed by the number of marbles originally placed at vertex v followed by a number d which is the number of children of v, followed by d numbers giving the identities of the children of v.

The input is terminated by a case where n = 0 and this case should not be processed.

Output

For each case in the input, output the smallest number of moves of marbles resulting in one marble at each vertex of the tree.

Sample input

 
9
1 2 3 2 3 4
2 1 0
3 0 2 5 6
4 1 3 7 8 9
5 3 0
6 0 0
7 0 0
8 2 0
9 0 0
9
1 0 3 2 3 4
2 0 0
3 0 2 5 6
4 9 3 7 8 9
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
9
1 0 3 2 3 4
2 9 0
3 0 2 5 6
4 0 3 7 8 9
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
0

Output for sample input

7
14
20

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct Node
{
    int root;
    int count;
    int child;
    int brother;
};
struct Node node[10010];

int out;

int search(int root)
{
    int count, child, marble;

    count = 0;
    child = node[root].child;
    while (child != -1)
    {
        marble = search(child);
        out += abs(marble);
        count += marble;
        child = node[child].brother;
    }
    count += node[root].count;
    
    return count - 1;
}

int main()
{
    int i, j, num, v, childs, child;

    while (scanf("%d", &num) && num)
    {
        for (i = 1; i <= num; ++i)
            node[i].root = 1;
        for (i = 1; i <= num; ++i)
        {
            scanf("%d", &v);
            scanf("%d", &node[v].count);
            node[v].child = -1;

            scanf("%d", &childs);
            for (j = 1; j <= childs; ++j)
            {
                scanf("%d", &child);
                node[child].root = 0;
                node[child].brother = node[v].child;
                node[v].child = child;
            }
        }
        for (i = 1; i <= num; ++i)
        {
            if (node[i].root == 1)
                break;
        }
        out = 0;
        search(i);
        printf("%d\n", out);
    }

    return 0;
}

阅读(2295) | 评论(0) | 转发(0) |
0

上一篇:Recaman's Sequence

下一篇:Letter Deletion

给主人留下些什么吧!~~