0% found this document useful (0 votes)
9 views

Problem - D - Codeforces

The document describes a problem from a Codeforces contest involving a tree with nodes that have value constraints. The goal is to perform operations to make all node values equal while minimizing the final value. Input includes multiple test cases with node constraints and edges, and the output is the minimum possible value for each test case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Problem - D - Codeforces

The document describes a problem from a Codeforces contest involving a tree with nodes that have value constraints. The goal is to perform operations to make all node values equal while minimizing the final value. Input includes multiple test cases with node constraints and edges, and the output is the minimum possible value for each test case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

3/9/25, 10:22 PM 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

Ethflow Round 1 (Codeforces


D. Balanced Tree Round 1001, Div. 1 + Div. 2)

time limit per test: 3 seconds Finished


memory limit per test: 512 megabytes
→ Practice?
You are given a tree∗ with n nodes and values li , r i for each node. You can choose an initial value ai
satisfying li ≤ ai ≤ r i for the i -th node. A tree is balanced if all node values are equal, and the value of Want to solve the contest problems after the
official contest ends? Just register for
a balanced tree is defined as the value of any node. practice and you will be able to submit
solutions.
In one operation, you can choose two nodes u and v, and increase the values of all nodes in the subtree

of node v by 1 while considering u as the root of the entire tree. Note that u may be equal to v. Register for practice

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.

Start virtual contest


9
Then n lines follow. The i -th line contains two integers li , r i (0 ≤ li ≤ r i ≤ 10 ) — the constraint of
the value of the i -th node.

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

a tree. dfs and similar dp graphs greedy

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

Example Announcement (en)


input Copy
Tutorial (en)
6
4
0 11
6 6
0 0
5 5
2 1
3 1
4 3
7
1 1
0 5
0 5
2 2
2 2
2 2
2 2
1 2
1 3
2 4
2 5
3 6
3 7
4
1 1
1 1
1 1
0 0
1 4
2 4
3 4
7
0 20

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] .

You can perform the following operations to make all ai equal:

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 ):

It can be proven that this is the optimal solution.

Codeforces (c) Copyright 2010-2025 Mike Mirzayanov


The only programming contests Web 2.0 platform
Server time: Mar/09/2025 22:21:47UTC+7 (l2).
Desktop version, switch to mobile version.
Privacy Policy

Supported by

https://codeforces.com/contest/2062/problem/D 4/4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy