#K71077. Strong Connectivity in Directed Graphs

    ID: 33451 Type: Default 1000ms 256MiB

Strong Connectivity in Directed Graphs

Strong Connectivity in Directed Graphs

Given a directed graph with ( n ) vertices and ( m ) edges, determine whether the graph is strongly connected. A directed graph is said to be strongly connected if, for every pair of vertices ( u ) and ( v ), there exists a path from ( u ) to ( v ) and a path from ( v ) to ( u ). In other words, it must be possible to reach any vertex from any other vertex.

The input starts with two integers ( n ) and ( m ), representing the number of vertices and edges respectively. This is followed by ( m ) lines, each containing two integers ( u ) and ( v ) which indicate a directed edge (road) from vertex ( u ) to vertex ( v ).

Your task is to output "YES" if the graph is strongly connected, and "NO" otherwise.

inputFormat

The first line of the input contains two space-separated integers ( n ) and ( m ). Each of the following ( m ) lines contains two space-separated integers ( u ) and ( v ) describing a directed edge from vertex ( u ) to vertex ( v ).

outputFormat

Output a single line containing "YES" if the graph is strongly connected or "NO" if it is not.## sample

4 5
1 2
2 3
3 4
4 1
2 4
YES