[Solution] Path Prefixes Codeforces Solution
You are given a rooted tree. It contains vertices, which are numbered from to . The root is the vertex .
Each edge has two positive integer values. Thus, two positive integers and are given for each edge.
Output numbers , where is defined as follows.
Consider the path from the root (vertex ) to (). Let the sum of the costs of along this path be . Then is equal to the length of the maximum prefix of this path such that the sum of along this prefix does not exceed .
Consider an example. In this case:
- , since the path to has an amount of equal to , only the prefix of this path of length has a smaller or equal amount of ;
- , since the path to has an amount
- of equal to , the prefix of length of this path has a sum of equal to ( the number is );
- , since the path to has an amount of equal to , the prefix of length of this path has an amount of equal to (this is the longest suitable prefix, since the prefix of length already has an amount of equal to , which is more than );
- , since the path to has an amount of equal to , the prefix of length of this path has a sum of equal to (this is the longest suitable prefix, since the prefix of length already has an amount of equal to , what is more than );
- , since the path up to has an amount of equal to , the prefix of length of this path has an amount of equal to ;
- , since the path to has an amount of equal to , the prefix of length of this path has an amount of equal to (this is the longest suitable prefix, since the prefix of length already has an amount of equal to , which is more than );
- , since the path up to has an amount of equal to , the prefix of length of this path has an amount of equal to ;
- , since the path to has an amount of equal to , the prefix of length of this path has a sum of equal to .
The first line contains an integer () — the number of test cases in the test.
The descriptions of test cases follow.
Each description begins with a line that contains an integer () — the number of vertices in the tree.
This is followed by string, each of which contains three numbers (; ) — the ancestor of the vertex , the first and second values an edge that leads from to . The value of runs through all values from to inclusive. It is guaranteed that each set of input data has a correct hanged tree with a root at the vertex .
It is guaranteed that the sum of over all input test cases does not exceed .
For each test case, output integer in one line: .
No comments:
Post a Comment