#C11152. Is the Graph a Tree?
Is the Graph a Tree?
Is the Graph a Tree?
You are given an undirected graph with n nodes and m edges. A graph is a tree if and only if it satisfies two conditions:
- The number of edges is exactly \( m = n - 1 \).
- The graph is connected, i.e. there is a path between any two vertices.
Your task is to determine whether a given graph is a tree. If both conditions are satisfied, output YES
; otherwise, output NO
.
Input format: The first line contains two integers n and m. The next m lines each contain two integers representing an undirected edge between the two given nodes.
Output format: Output a single line containing YES
if the graph is a tree, otherwise NO
.
inputFormat
The input is given from standard input (stdin) and is structured as follows:
n m u1 v1 ... um vm
where n is the number of nodes, m is the number of edges, and each subsequent line contains two integers u and v representing an edge between nodes u and v.
outputFormat
The output should be printed to the standard output (stdout) as a single line containing either YES
if the graph is a tree, or NO
otherwise.
4 3
1 2
2 3
3 4
YES
</p>