[Solution] Fake Plastic Trees Codeforces Solution | Codeforces Problem Solution 2022
We are given a rooted tree consisting of vertices numbered from to . The root of the tree is the vertex and the parent of the vertex is .
There is a number written on each vertex, initially all numbers are equal to . Let's denote the number written on the vertex as .
For each , we want to be between and .
In a single operation we do the following:
- Choose some vertex . Let be vertices on the path from the vertex to vertex (meaning , and ).
- Choose a non-decreasing array of length of nonnegative integers: .
- For each , increase by .
What's the minimum number of operations needed to achieve our goal?
The first line contains an integer — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer — the number of the vertices in the tree.
The second line of each test case contains integers, , where denotes the parent of the vertex .
The -th of the following lines contains two integers and .
It is guaranteed that the sum of over all test cases doesn't exceed .
For each test case output the minimum number of operations needed.
No comments:
Post a Comment