#K91627. Tree Verification
Tree Verification
Tree Verification
You are given an undirected graph with N nodes and M edges. Your task is to determine whether the graph forms a tree. Recall that a tree is a connected acyclic graph that always satisfies \( M = N - 1 \). In other words, for the graph to be a tree, it must:
- Have exactly \( N - 1 \) edges.
- Be connected (there is a path between every pair of nodes).
If both conditions are met, output YES
; otherwise, output NO
.
inputFormat
The input is read from standard input (stdin):
N M u1 v1 u2 v2 ... vM
Here, N
is the number of nodes, M
is the number of edges, and each of the next M
lines contains two integers u
and v
representing an undirected edge between nodes u
and v
.
outputFormat
The output should be printed to standard output (stdout) and is a single line containing either YES
if the graph is a tree, or NO
otherwise.
5 4
1 2
2 3
4 3
5 3
YES