#K43252. Graph Connectivity Check
Graph Connectivity Check
Graph Connectivity Check
You are given an undirected graph with n nodes and m edges. Your task is to determine whether the graph is connected, i.e. there is a path between every pair of distinct nodes.
The input will begin with two integers n and m. Then, m lines follow, each containing two integers u and v which represent an edge connecting node u and node v. If the graph is connected, print YES
, otherwise print NO
.
Note: If there is only one node and no edges, the graph is trivially connected.
The formula for connectivity is not explicitly needed here, but you may refer to the formal definition: A graph G is connected if for every pair of vertices u and v, there exists a path between u and v. In LaTeX, this is written as:
\(\forall u,v \in V, \ \exists \text{ a path } u \rightarrow v\)
inputFormat
The first line of input contains two space-separated integers n
and m
, representing the number of nodes and edges respectively. The next m
lines each contain two integers u
and v
, denoting an edge between nodes u
and v
.
It is guaranteed that 1 ≤ n ≤ 105 and 0 ≤ m ≤ 105.
outputFormat
Output a single line containing YES
if the graph is connected, otherwise output NO
.
4 2
1 2
3 4
NO
</p>