Problem - E1 - Codeforces
Problem - E1 - 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
In each turn, assuming the opponent chooses j in the last turn, the player can choose any remaining
node i satisfying wi > wj and delete the subtree† of node i . In particular, Cirno can choose any node → Virtual participation
and delete its subtree in the first turn.
Virtual contest is a way to take part in past
contest, as close as possible to participation
The first player who can not operate wins, and they all hope to win. Find one of the possible nodes on time. It is supported only ICPC mode for
Cirno may choose so that she wins if both of them play optimally. virtual contests. If you've seen these
problems, a virtual contest is not for you -
solve these problems in the archive. If you
∗
A tree is a connected graph without cycles.
just want to solve some problem from a
†
Node u is considered in the subtree of node i if any path from 1 to u must go through i. contest, a virtual contest is not for you -
solve this problem in the archive. Never use
Input someone else's code, read the tutorials or
communicate with other person during a
The first line of input contains a single integer t (1
5
≤ t ≤ 10 ) — the number of input test cases. virtual contest.
https://codeforces.com/contest/2062/problem/E1 1/3
3/9/25, 10:22 PM Problem - E1 - Codeforces
The second line contains n integers w1 , w2 , … , wn (1 ≤ wi ≤ n ) — the value of each node. → Problem tags
The next n − 1 lines contain the edges of the tree. The i -th line contains two integers u i , vi (
data structures dfs and similar games
1 ≤ u i , vi ≤ n , u i ≠ vi ) — an edge connecting u i and vi . It is guaranteed that the given edges form
graphs greedy trees *2000
a tree.
No tag edit access
It is guaranteed that the sum of n over all test cases does not exceed 4 ⋅ 10 .
5
Example
input Copy
5
4
2 2 4 3
1 2
1 3
2 4
5
1 2 3 4 5
1 2
2 3
3 4
4 5
3
1 2 3
1 2
1 3
5
3 1 3 4 5
1 2
2 3
3 4
4 5
10
1 2 3 2 4 3 3 4 4 3
1 4
4 6
7 4
6 9
https://codeforces.com/contest/2062/problem/E1 2/3
3/9/25, 10:22 PM Problem - E1 - Codeforces
6 5
7 8
1 2
2 3
2 10
output Copy
2
0
2
2
10
Note
In the first test case:
1. If Cirno chooses 1 or 3 in the first turn, Daiyousei cannot make a move, so Daiyousei wins.
2. If Cirno chooses 2 or 4 in the first turn, Daiyousei can only choose 3, but after it Cirno cannot make a
move, so Cirno wins.
In the second test case, regardless of which node Cirno chooses, Daiyousei cannot make a move, so
Daiyousei wins.
In the third and fourth test case, the only possible node Cirno may choose in the first turn is 2.
In the fifth test case, all possible nodes Cirno may choose in the first turn are 3, 4, 6, 7 and 10 .
Supported by
https://codeforces.com/contest/2062/problem/E1 3/3