#C5551. City Road Connectivity

    ID: 49213 Type: Default 1000ms 256MiB

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 integers u and v, representing a street going from intersection u to intersection v.

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>