#C3841. Rotation-Proof Highway Network
Rotation-Proof Highway Network
Rotation-Proof Highway Network
You are given a directed graph representing an aerial highway network. The network is considered rotation-proof if and only if there exists a designated starting station (station 1) such that every other station is reachable from station 1 and station 1 is also reachable from every other station. In other words, the graph is strongly connected.
The first line of input contains two integers n and m, where n is the number of stations and m is the number of highways. Each of the next m lines contains two integers u and v which represent a directed highway from station u to station v.
Your task is to determine whether the highway network is rotation-proof. If it is, output YES
; otherwise, output NO
.
Note: The necessary condition is that the network must be strongly connected. Additionally, if the number of highways is less than the number of stations, the network cannot be rotation-proof.
inputFormat
The input is given from standard input and is formatted as follows:
n m u1 v1 u2 v2 ... um vm
Where:
- n (1 ≤ n ≤ 105) is the number of stations.
- m (1 ≤ m ≤ 105) is the number of highways.
- Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n), indicating a directed highway from station u to station v.
outputFormat
Print a single line to standard output containing YES
if the network is rotation-proof (i.e. strongly connected), or NO
otherwise.
4 5
1 2
2 3
3 4
4 1
1 3
YES