This page is still under construction.

Parts of this page are still being built. What you see may change.

Diplomacy

Time limit1sMemory limit256 MB

Summary
Each month one same-party friend group switches parties with the two sides alternating, and the goal is the fewest months to unite all governors in one party.
Level

Medium7 of 10

Topics
Graph, Shortest path, BFS
Solved
No attempts yet

Problem

You are a senator in an ancient empire ruled by a dictator. You have joined a secret committee of senators from both parties that is plotting to overthrow him. The plot succeeds only if every state in the empire backs it, and that requires every state governor to belong to the same party.

Right now each governor belongs to either the Orange Party or the Purple Party. You are confident that you can get either party behind the plot, so it does not matter which party wins out in the end.

The committee has studied the political situation. Two governors influence each other when they are friends and belong to the same party. Each month one lobbyist does whatever it takes to make a single governor switch parties. When that happens, every friend of that governor who belonged to the same party switches as well, and so do the friends of those friends who belonged to that party, and the change keeps spreading. To avoid suspicion the committee alternates Orange and Purple lobbyists from month to month. In the first month it may start with either party.

The committee also knows which governors are friends. The friendship graph is connected, so there is no isolated group whose members are friends only with each other.

Find the minimum number of months needed for every governor to belong to the same party.

Input

The input contains several test cases. The first line of each test case has two integers nn and mm (1≤n≤1001 \le n \le 100, n−1≤m≤n(n−1)/2n - 1 \le m \le n(n-1)/2), where nn is the number of governors and mm is the number of known friendships. The second line has nn integers, each 0 or 1, giving the current party of governors 1 through nn in order. 0 is Orange and 1 is Purple. Each of the next mm lines has two integers aa and bb (1≤a<b≤n1 \le a < b \le n), meaning that governor aa and governor bb are friends. Friendship goes both ways: if aa is a friend of bb, then bb is a friend of aa. All mm pairs (a,b)(a, b) are distinct. In every test case the friendship graph is connected. The input ends with a line holding two zeros, and that line is not a test case.

Output

For each test case, print one integer, the minimum number of months needed for every governor to belong to the same party. Print one integer per line, and do not print blank lines between them.

Examples3

  1. Example 1

    Input
    4 3
    0 1 0 0
    1 2
    2 3
    2 4
    5 4
    0 1 1 0 1
    1 2
    2 3
    3 4
    4 5
    0 0
    
    Expected output
    1
    2
    
  2. Example 2

    Input
    3 2
    1 1 1
    1 2
    2 3
    0 0
    
    Expected output
    0
    
  3. Example 3

    Input
    1 0
    0
    2 1
    0 1
    1 2
    2 1
    1 1
    1 2
    0 0
    
    Expected output
    0
    1
    0