Hostile States
Time limit1sMemory limit128 MB
Assign each undecided city to one of two states to minimize the number of roads crossing between states.
- Level
Medium7 of 10
- Topics
- Graph
- Solved
- No attempts yet
Problem
Bitocja and Bajtocja are about to sign a truce after a long war. The two states must decide which state each city belongs to. Their rulers agreed to split the cities so as to minimize the number of roads that directly connect a pair of cities belonging to different states.
After the truce is signed, report how many such roads connect the two states.
Input
The first line contains the number of cities and the number of roads (, ).
The second line contains integers (). If , city belongs to Bitocja; if , city belongs to Bajtocja; if , city must be assigned to either Bitocja or Bajtocja.
Each of the next lines contains two integers , (), meaning that cities and are joined by a direct road. No pair appears more than once.
Output
Print a single integer: the minimum possible number of roads connecting a city of Bitocja with a city of Bajtocja after the truce is signed.
Hint
In the first example, city 3 is the only unassigned city. Assigning it to Bitocja leaves just one road between the two states, the road between cities 3 and 4, whereas assigning it to Bajtocja would leave two such roads. Hence the minimum is 1.