#C8471. Are All Active Members Connected?

    ID: 52457 Type: Default 1000ms 256MiB

Are All Active Members Connected?

Are All Active Members Connected?

You are given an undirected graph with n nodes and m edges. Each node represents a member and has an associated state: active (denoted by 1) or inactive (denoted by 0). Determine whether all active members in the graph are connected by some path.

Two active members are considered connected if there exists a path (which may include inactive members) between them. If there are no active members, consider the graph as trivially connected.

Formally, let \( a_i \) be the state of the \( i^{th} \) member, where \( a_i \in \{0,1\} \). Given the undirected edges, check if for every pair of indices \( i, j \) with \( a_i = 1 \) and \( a_j = 1 \), there exists a path between node \( i \) and node \( j \).

Output YES if the active members are all connected, otherwise output NO.

inputFormat

The input is given from stdin in the following format:

n m
a1 a2 ... an
u1 v1
u2 v2
... 
um vm

Where:

  • n is the number of nodes.
  • m is the number of edges.
  • The second line contains n integers, where the i-th integer is 1 if the member is active and 0 otherwise.
  • Each of the next m lines contains two integers u and v representing an undirected edge between nodes u and v (nodes are 1-indexed).

outputFormat

Output a single line to stdout with either YES or NO indicating whether all active members are connected.

## sample
5 4
1 0 1 1 0
1 2
2 3
3 4
4 5
YES