#C7337. Check if Graph is a Tree

    ID: 51197 Type: Default 1000ms 256MiB

Check if Graph is a Tree

Check if Graph is a Tree

You are given an undirected graph with \( n \) nodes and \( m \) edges. The graph is provided as a list of \( m \) edges. Your task is to determine whether the provided graph is a tree.

A graph is a tree if and only if:

  • It is connected, meaning there is a path between every pair of vertices.
  • It has exactly \( n-1 \) edges.

Input is read from standard input and output is printed to standard output. The first line contains two integers, \( n \) and \( m \). Each of the next \( m \) lines contains two integers representing an edge between two nodes.

Your answer should print "YES" if the graph is a tree and "NO" otherwise.

inputFormat

The first line of the input contains two integers \( n \) and \( m \), where \( n \) is the number of nodes and \( m \) is the number of edges. The following \( m \) lines each contain two space-separated integers denoting an edge between two nodes.

Constraints:

  • \( 1 \le n \le 10^5 \)
  • \( 0 \le m \le 10^5 \)

outputFormat

Output "YES" if the given graph is a tree and "NO" otherwise. The result should be printed to the standard output.

## sample
5 4
1 2
1 3
2 4
2 5
YES