#K81592. Network Connectivity
Network Connectivity
Network Connectivity
You are given a network of n computers numbered from 1 to n and m bidirectional direct communication links connecting pairs of computers. Your task is to determine whether the entire network is connected, i.e. if every computer can communicate with every other computer either directly or indirectly through a series of links.
The network is represented by n and m followed by m pairs of integers. Each pair represents a direct link between two computers.
If the network is connected, print YES
. Otherwise, print NO
.
Note: A network with only one computer is always considered connected.
inputFormat
The first line of the input contains two integers n and m where:
- n (1 ≤ n ≤ 105): The number of computers.
- m (0 ≤ m ≤ 105): The number of direct communication links.
The next m lines each contain two space-separated integers u and v ( 1 ≤ u, v ≤ n, u ≠ v) representing a bidirectional link between computers u and v.
It is guaranteed that each link appears at most once.
outputFormat
Output a single line with YES
if the network is connected, and NO
otherwise.
5 4
1 2
2 3
3 4
4 5
YES