#K1266. Villagers Connectivity Check
Villagers Connectivity Check
Villagers Connectivity Check
Given n villagers and q friendship pairs, determine whether all villagers are connected, either directly or indirectly. Two villagers are said to be connected if there exists a sequence of friendships connecting them. In other words, the friendship network forms a single connected component.
You are given a graph with n nodes and q edges. The connectivity condition can be expressed in \( \text{latex} \) as:
\( \forall v \in \{1, 2, \dots, n\}, \; \exists\; \text{a path from } 1 \text{ to } v \).
If all villagers are interconnected, print YES
; otherwise, print NO
.
inputFormat
The first line of input contains two integers n
and q
where n
is the number of villagers and q
is the number of friendship pairs.
Each of the next q
lines contains two integers u
and v
representing a friendship between villagers u
and v
.
Note: The villagers are numbered from 1 to n
.
outputFormat
Output a single line containing YES
if all villagers are connected, otherwise output NO
.
5 4
1 2
2 3
4 5
3 4
YES