CiscoOA #11
Collect Coins
Estimated HardJudge-ready
Topics (AI-labelled)Topo SortUnion-Find
Each vertex of an undirected tree holds 0 or 1 coin. You may choose any starting vertex; whenever you reach a vertex, you collect every coin within graph distance at most 2. You move along edges and must eventually return to the starting vertex. Find the minimum number of edges traversed to collect all the coins.
Input
The first line contains n, followed by n values of 0/1, followed by n−1 undirected 0-based edges, guaranteed to form a tree. The original gave no numeric bounds; this site adds 1≤n≤200000.
Output
Output a single integer, the answer.
Example 1
Input:
6
1 0 0 0 0 1
0 1
1 2
2 3
3 4
4 5
Output:
2
Example 2
Input:
4
1 1 1 1
0 1
0 2
0 3
Output:
0
Example 3
Input:
1
1
Output:
0
Example notes
The outputs of the three public examples are, in order: 2; 0; 0. Verified by independent enumeration or direct simulation.
Editorial
Loading solution…