#K10196. Tree Validation
Tree Validation
Tree Validation
Given an undirected graph with (n) nodes and (m) edges, determine whether the provided set of edges forms a valid tree. A valid tree is a connected acyclic graph that satisfies the condition (m = n - 1). Note that the nodes are numbered from 1 to (n). If the number of edges is not exactly (n-1), or if adding an edge creates a cycle, then the graph does not form a tree.
In this problem, you are provided with (n), (m) and a list of (m) edges. Your task is to check if these edges, when taken in the given order, form a tree. The input is given via standard input and the output should be printed to standard output as either "YES" or "NO".
inputFormat
The first line contains two integers (n) and (m), representing the number of nodes and edges respectively ((1 \leq n \leq 10^5), (0 \leq m \leq 10^5)). The next (m) lines each contain two integers (u) and (v) (1-indexed), representing an undirected edge between nodes (u) and (v).
outputFormat
Print a single line containing "YES" if the given edges form a valid tree; otherwise, print "NO".## sample
5 4
1 2
2 3
3 4
4 5
YES