#C7865. Check if a Graph is a Tree
Check if a Graph is a Tree
Check if a Graph is a Tree
You are given a graph with M landmarks (nodes) and R roads (edges). Each road connects two distinct landmarks. Your task is to determine whether these landmarks and roads form a tree.
A graph is a tree if and only if:
- It is connected, i.e. every node is reachable from any other node.
- It has no cycles.
- It has exactly \( M-1 \) edges.
Note: A graph with a single node and zero edges is also considered a tree.
inputFormat
The first line of input contains two integers M and R, where M is the number of landmarks and R is the number of roads. The following R lines each contain two integers u and v indicating that there is a road connecting landmark u and landmark v.
Constraints:
- 1 \(\leq M \leq 10^5\)
- 0 \(\leq R \leq 10^5\)
outputFormat
Output a single line containing "YES" if the graph forms a tree, and "NO" otherwise.
## sample5 4
1 2
2 3
3 4
4 5
YES