cswork / OAOpen practice workspace

SalesforceOA #20

Get Node To Remove

Estimated MediumJudge-ready

Topics (AI-labelled)Union-Find

Delete exactly one node together with its incident edges, then let the infection spread along undirected edges from the remaining initially infected nodes until no new node is infected. A healthy node may be deleted. Find the deletion that minimizes the final number of infected nodes, breaking ties by the smallest 1-based number.

Input

The first line contains n m, the second line contains n initial infection flags, each 0/1, and then m lines follow with 1-based edges u v. 1≤n≤1000, 0≤m≤min(n(n−1)/2,1000); this site treats the graph as a simple undirected graph, with no duplicate edges and no self-loops.

Output

Output the number of the node to delete. Even if there is no infection, one node must still be deleted.

Example 1

Input:

9 5
0 0 1 0 1 0 0 0 0
1 2
2 3
4 5
6 7
7 8

Output:

3

Example 2

Input:

5 4
1 1 1 1 1
1 2
2 3
3 4
4 5

Output:

1

Example 3

Input:

6 5
1 1 0 0 0 0
1 3
2 3
3 4
4 5
5 6

Output:

3

Example notes

The answers are 3, 1, and 3. In the third example, healthy node 3 is a cut vertex, and deleting it is better than deleting any single infection source. Original example 1 omitted node 1 from its spread set; the deletion answer 3 is unchanged.

Editorial

Loading solution…