#K59557. Network Connectivity Problem

    ID: 30890 Type: Default 1000ms 256MiB

Network Connectivity Problem

Network Connectivity Problem

You are given an undirected network of n stations and m direct connections. The network can be represented as an undirected graph where the stations are nodes and each connection is an edge between two nodes.

Your task is to determine whether the entire network is connected; in other words, whether there exists a path between every pair of stations. If the graph is connected, output YES, otherwise output NO.

Mathematically, the network is connected if for every pair of stations \( u,v \), there exists a sequence of stations \( u = x_0, x_1, \dots, x_k = v \) such that for each \( i \), station \( x_i \) is directly connected to \( x_{i+1} \). Formally, the graph is connected if \( \forall u,v \in \{1,2,\dots,n\},\, \exists\; \text{a path from } u \text{ to } v \).

inputFormat

The input is read from stdin and has the following format:

  • The first line contains two integers \( n \) and \( m \), where \( n \) is the number of stations and \( m \) is the number of direct connections.
  • The next \( m \) lines each contain two integers \( u \) and \( v \), indicating that there is a direct connection between station \( u \) and station \( v \).

outputFormat

Output a single line to stdout containing either YES if the network is fully connected, or NO otherwise.

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