#C2626. Valid Tree
Valid Tree
Valid Tree
You are given n distinct nodes and m undirected edges. Your task is to determine whether the given set of edges forms a valid tree.
A graph is a tree if and only if it is connected and does not contain a cycle. In other words, the necessary and sufficient condition is that the number of edges equals n - 1
(i.e. \(m = n - 1\)) and there is no cycle in the graph.
Note: The nodes are numbered from 1 to n.
inputFormat
The input is given via standard input (stdin) and consists of:
- One line with two space-separated integers n and m, representing the number of nodes and edges respectively.
- Then m lines follow, each containing two space-separated integers u and v which represent an edge connecting node u to node v.
outputFormat
Output via standard output (stdout) a single line containing either Yes
if the given edges form a valid tree, or No
otherwise.
4 3
1 2
2 3
3 4
Yes