3/9/25, 10:22 PM 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
Ethflow Round 1 (Codeforces
E1. The Game (Easy Version) Round 1001, Div. 1 + Div. 2)
time limit per test: 4 seconds Finished
memory limit per test: 512 megabytes
→ Practice?
This is the easy version of the problem. The difference between the versions is that in this
version, you only need to find one of the possible nodes Cirno may choose. You can hack only if Want to solve the contest problems after the
official contest ends? Just register for
you solved all versions of this problem. practice and you will be able to submit
solutions.
Cirno and Daiyousei are playing a game with a tree∗ of n nodes, rooted at node 1. The value of the i -th
node is wi . They take turns to play the game; Cirno goes first. Register for practice
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.
The first line of each test case contains one integer n (1 ≤ n ≤ 4 ⋅ 10
5
) — the number of nodes in the Start virtual contest
tree.
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
Output → Contest materials
For each test case, print one line.
Announcement (en)
If Cirno wins the game, print any possible node she may choose in the first turn.
Tutorial (en)
Otherwise, print "0" (without quotes).
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.
Therefore, all possible nodes Cirno may choose are 2 and 4.
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 .
Codeforces (c) Copyright 2010-2025 Mike Mirzayanov
The only programming contests Web 2.0 platform
Server time: Mar/09/2025 22:21:48UTC+7 (l2).
Desktop version, switch to mobile version.
Privacy Policy
Supported by
https://codeforces.com/contest/2062/problem/E1 3/3