10245_Marblesonatree

2022-5-16 18:16| 发布者: Hocassian| 查看: 80| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-3-89504693774100-Problem List-采集的数据-后羿采集器.html

Pro.ID

10245

Title

Marbles on a tree

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=10245

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 1000/500 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others)
  • 描述

    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?

    输入

    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.

    输出

    Description

    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
    Sample Output
    7
    14
    20
    Source

    样例输入

    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

    样例输出

    7
    14
    20

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部