RoAlgo Contest #9 | City Skylines

This was the problem page during the contest. Access the current page here.
Time limit: 1s Memory limit: 64MB Input: Output:

Task

JellyTheOctopus has just gotten the game City Skylines! The goal of the game is to create a prospering city. So far, Jelly's city has NN districts (named from 11 to NN) connected by MM different connection roads (two districts have at most one undirected road traveling between them).

There are three types of districts: industry, business, and living. An industry district is where the city gets its resources (e.g. from mining, logging, farming, etc.). No one wants to deal with coal mines, so every industry district contributes 1-1 points to the total prosperity of Jelly's city. A business district is where all the companies and jobs of the city are located. As this is the money-making part of the city, each business district contributes 11 point to the total prosperity of Jelly's city. A living district is where all the houses and apartments are located. These are just a basic necessity, so each living district contributes 00 points to the total prosperity of the city.

Not only does Jelly care about the overall prosperity of his city, he also wants his city to be balanced. The definition of a balanced city is as follows: for every district aia_i, the prosperity of district aia_i must be equal to the sum of the prosperity of all adjacent districts (meaning all districts connected to aia_i by a single road). The current state of Jelly's city may not be balanced. For this reason, he asks you to help him make his city balanced. You can add new districts as follows: take any district already in Jelly's city, then connect a completely new district (of any type) with a road. Once you add a new district, it becomes a part of Jelly's city (so you could add another new district to the one you just created). Also, you must add a single city at a time.

Help Jelly find the minimum number of new districts needed to create a balanced city.

Input data

On the first line there are two integers, NN and MM, representing the number of districts, and the number of roads connecting two districts together.

The next line consists of NN integers, aia_i, representing the type of each district (either a 1-1, 00, or 11).

The next MM lines consists of two integers, uu and vv, representing an undirected road between districts uu and vv.

Output data

Output a single integer KK, the minimum number of districts needed for Jelly to have a balanced city.

Constraints and clarifications

  • 1N1051 \leq N \leq 10^5;
  • 1Mmin(105,N(N1)2)1 \leq M \leq \min(10^5, \frac{N \cdot (N-1)}{2});
  • 1ai1-1 \leq a_i \leq 1;
  • Each pair of districts have at most one road connecting them.
  • By the nature of adding new districts, if there are KK new districts, there should be exactly KK new roads between districts.
# Score Constraints
0 0 Example
1 40 N100N \le 100
2 60 No additional constraints

Example

stdin

3 2
0 1 -1
1 2
1 3

stdout

2

Explanation

Further clarification about adding new districts: If, for example, Jelly has a city of 55 districts, you could add a new district to district 55. (Thus, there is now a road between district 55 and district 66, and Jelly's city is now consisted of 66 districts). District 66 is now a part of Jelly's city, and you cannot create a road from district 66 to districts 11 through 55. However, because district 66 is now a part of the city, you can now add a new city to district 66 (e.g. a new road between district 66 and district 77).

Log in or sign up to be able to send submissions!