#K64747. Consistent Animal Friendships

    ID: 32044 Type: Default 1000ms 256MiB

Consistent Animal Friendships

Consistent Animal Friendships

In this problem, you are given information about a group of animals and the friendships between them. Each animal has a status which can be either 0 or 1. The animals form an undirected graph where each edge represents a friendship. Your task is to determine whether every connected component in this graph is consistent; that is, all animals in any single component must share the same status. Formally, let (G = (V, E)) be an undirected graph with (N) nodes and (M) edges. Additionally, each node (i) ((1 \le i \le N)) has an associated status (s_i \in {0, 1}). The graph is consistent if for every connected component (C) of (G), (s_i = s_j) for all (i, j \in C). Output (YES) if the graph is consistent; otherwise, output (NO).

inputFormat

The input is provided via standard input (stdin) with the following format:

  • The first line contains two integers (N) and (M), where (N) is the number of animals and (M) is the number of friendships.
  • The second line contains (N) integers representing the statuses of the animals, given in order from animal 1 to animal (N). Each status is either 0 or 1.
  • The next (M) lines each contain two integers (a) and (b) (1-indexed), indicating that there is a friendship between animal (a) and animal (b).

outputFormat

Output a single line to standard output (stdout) with either (YES) if every connected component has consistent statuses, or (NO) if at least one connected component has conflicting statuses.## sample

4 3
1 1 1 1
1 2
2 3
3 4
YES