Problem - D - Codeforces
Problem - D - Codeforces
Enter | Register
HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN
Codeforces Blitz Cup 2025 Final Rounds Have Started! Join the stream now: https://youtu.be/rbHFLNg7Nkc. ×
PROBLEMS SUBMIT CODE MY SUBMISSIONS STATUS HACKS ROOM STANDINGS CUSTOM INVOCATION
Your goal is to perform a series of operations so that the tree becomes balanced. Find the minimum
possible value of the tree after performing these operations. Note that you don't need to minimize the → Virtual participation
number of operations.
Virtual contest is a way to take part in past
contest, as close as possible to participation
∗
A tree is a connected graph without cycles.
on time. It is supported only ICPC mode for
†
Node w is considered in the subtree of node v if any path from root u to w must go through v . virtual contests. If you've seen these
problems, a virtual contest is not for you -
Input solve these problems in the archive. If you
just want to solve some problem from a
The first line of input contains a single integer t (1
5
≤ t ≤ 10 ) — the number of input test cases. contest, a virtual contest is not for you -
solve this problem in the archive. Never use
The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10
5
) — the number of nodes in the someone else's code, read the tutorials or
communicate with other person during a
tree. virtual contest.
https://codeforces.com/contest/2062/problem/D 1/4
3/9/25, 10:22 PM Problem - D - Codeforces
The next n − 1 lines contain the edges of the tree. The i -th line contains two integers u i , vi ( → Problem tags
1 ≤ u i , vi ≤ n , u i ≠ vi ) — an edge connecting u i and vi . It is guaranteed that the given edges form
5 trees *2200
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10 .
No tag edit access
Output
For each test case, output a single integer — the minimum possible value that all ai can be made equal
to after performing the operations. It can be shown that the answer always exists. → Contest materials
https://codeforces.com/contest/2062/problem/D 2/4
3/9/25, 10:22 PM Problem - D - Codeforces
0 20
0 20
0 20
3 3
4 4
5 5
1 2
1 3
1 4
2 5
3 6
4 7
5
1000000000 1000000000
0 0
1000000000 1000000000
0 0
1000000000 1000000000
3 2
2 1
1 4
4 5
6
21 88
57 81
98 99
61 76
15 50
23 67
2 1
3 2
4 3
5 3
6 4
output Copy
11
3
3
5
3000000000
98
Note
In the first test case, you can choose a = [6, 6, 0, 5] .
https://codeforces.com/contest/2062/problem/D 3/4
3/9/25, 10:22 PM Problem - D - Codeforces
1. Choose u ,
= 4 v = 3 and perform the operation 5 times.
2. Choose u = 1, v = 3 and perform the operation 6 times.
The complete process is shown as follows (where the numbers inside the parentheses are elements of
a ):
Supported by
https://codeforces.com/contest/2062/problem/D 4/4