#K72412. Taco's One-Way Street Connectivity
Taco's One-Way Street Connectivity
Taco's One-Way Street Connectivity
You are given a directed graph representing one-way streets between intersections. The graph has n intersections labelled from 1 to n and m one-way streets. Each street connects two intersections and is given as a directed edge from a to b.
Your task is to determine if it is possible to travel from any intersection to any other intersection. In other words, the graph must be strongly connected. Formally, a directed graph \(G=(V,E)\) is strongly connected if for every pair of vertices \(u,v\in V\) there exists a path from \(u\) to \(v\).
Note that when there is only one intersection (i.e. \(n=1\)), the answer is trivially YES
.
Input/Output via standard input and output.
inputFormat
The input is given via standard input and has the following format:
n m a1 b1 a2 b2 ... am bm
Where:
n
is the number of intersections.m
is the number of one-way streets.- Each of the next
m
lines contains two integersa
andb
indicating a one-way street from intersectiona
to intersectionb
.
outputFormat
Output a single line containing either YES
if the graph is strongly connected, or NO
otherwise. Output is via standard output.
4 5
1 2
2 3
3 4
4 1
2 4
YES