#C5903. Road Network Connectivity
Road Network Connectivity
Road Network Connectivity
You are given a directed graph representing a network of intersections and one-way roads. The graph has n intersections numbered from 1 to n and m directed roads. Your task is to determine whether it is possible to visit every intersection starting from intersection 1 and, additionally, whether every intersection can reach intersection 1. In other words, the graph should be strongly connected with respect to node 1.
The input guarantees that the nodes are indexed from 1 to n. Use breadth-first search (BFS) or depth-first search (DFS) to check connectivity in the original graph and its reverse.
Note: When presenting a formula, use LaTeX formatting. For instance, the number of intersections and roads can be represented as \( n \) and \( m \), respectively.
inputFormat
The input is given from standard input in the following format:
n m a1 b1 a2 b2 ... am bm
Where the first line contains two integers \( n \) and \( m \) — the number of intersections and the number of roads. Each of the next \( m \) lines contains two integers \( a_i \) and \( b_i \), indicating a directed road from intersection \( a_i \) to intersection \( b_i \).
outputFormat
Output a single line to standard output containing either YES
if it is possible to travel from intersection 1 to every other intersection and back, or NO
otherwise.
4 4
1 2
2 3
3 4
4 1
YES