Time limit: 1 second Memory limit: 256 MB The president of Kodakp wants to give each…
- User
Time limit: 1 second Memory limit: 256 MB The president of Kodakp wants to give each governor a number of coins. He wants to give governors in two neighboring provinces different amounts of coins to create competition between provinces.
Description of the image
The roads of this country are two-way and are stretched between two provinces. We know that if we start from every province of the country, we can reach all the provinces of the country by going to the neighboring provinces. Also, the number of roads is very small and it is designed as the minimum number of roads to reach from all provinces to other provinces. It can be proved that the number of roads is always one less than the number of provinces.
Calculate the minimum number of coins that the president must spend to reward the governors in such a way that the governors of no two neighboring provinces have received an equal number of coins.
input In the first line of input, integer and positive number t t indicates the number of scenarios. Then, for each scenario, the inputs are given in sequence. 1 ≤ t ≤ 300 , 000 1≤t≤300,000
In the first line of each scenario, an integer and positive number n n which indicates the number of cities in the scenario i This is the input given. Then in n
1 n−1 next line of roads of the input country are given, in each line two integers and positive numbers v i v i and u i u i It is given that two provinces determine the origin and destination of the road.
1 ≤ n ≤ 300 , 000 1≤n≤300,000 1 ≤ u i , v i ≤ n 1≤u i v i ≤n
It is guaranteed that the total n n should be at most 300,000 on all scenarios.
output output t t has a line that for each scenario, you should print in one line the minimum amount of coins to reward all provinces.
examples Sample entry 1 2 2 2 1 8 7 3 4 6 1 7 5 4 7 4 2 7 8 4 Plain text Sample output 1 3 11 Plain text Explanation of sample 1 Description of the image
If to province 1, 1 1 coin and to province 2, 2 Given 2 coins, no two adjacent provinces will receive the same number of coins, and this is the lowest possible coin total. So the answer is equal 1 + 2
3 1+2=3.
Explanation of sample 2 Description of the image
If to province 1, 2, 3, 5, 6 and 8 1 1 coin and to province 4, 2 2 coins and to province 7, 3 Given 3 coins, no two adjacent provinces will receive the same number of coins, and this is the lowest possible coin total. So the answer is equal 6 x 1 + 2 + 3
11 6x1+2+3=11. write code in java17
- Assistant