#C5551. City Road Connectivity
City Road Connectivity
City Road Connectivity
You are given a city with n intersections and m one-way streets. Each street connects two intersections. The task is to determine whether the entire city is strongly connected. In other words, for any two intersections \(u\) and \(v\), there must exist a path from \(u\) to \(v\) and a path from \(v\) to \(u\).
A directed graph is said to be strongly connected if for every pair of vertices \(u, v\), there is a path from \(u\) to \(v\). Use this fact to check if the given city structure meets the requirement.
inputFormat
The input is given from stdin and has the following format:
n m u1 v1 u2 v2 ... um v m
Where:
n
is the number of intersections (vertices).m
is the number of one-way streets (directed edges).- Each of the next
m
lines contains two integersu
andv
, representing a street going from intersectionu
to intersectionv
.
outputFormat
Output a single line to stdout:
YES
if the city is strongly connected; otherwise, output
NO## sample
5 6
1 2
2 3
3 4
4 5
5 1
2 4
YES
</p>