An Old Stone Game
Time limit1sMemory limit128 MB
For each of up to 10 general trees, compute the minimum number of stones needed so that starting from the bucket you can place a stone on the root by combining stones at fully occupied siblings.
- Level
Medium7 of 10
- Topics
- Tree, Greedy, Sorting, Dynamic programming
- Solved
- No attempts yet
Problem
There is an old stone game played on an arbitrary general tree . The goal is to place a single stone on the root of while obeying the following rules:
- At the start of the game, the player picks stones and puts them all into one bucket.
- At each step, the player may take one stone from the bucket and place it on any empty leaf.
- When every one of the immediate children of a node holds one stone, the player may remove all of these stones and place one of them on . The remaining stones are returned to the bucket and can be reused in later steps.
The player wins if, by following these rules, a stone is successfully placed on the root of the tree.
Write a program that determines the least number of stones the player must pick at the beginning so that the game on the given tree can be won.
Input
The input describes several trees. The first line contains , the number of trees (). Descriptions of the trees follow. Each tree has nodes labeled , and each node may have any number of children. The root is labeled . Each tree's description begins with on its own line. The next lines describe the children of every node in order of their labels; each line contains a node label (), the number of immediate children of , and then the labels of those children.
Output
For each input tree, print one line containing the minimum number of stones that must be picked in rule 1 in order to win the game on that tree.