#K65317. Check if Graph is a Tree

    ID: 32171 Type: Default 1000ms 256MiB

Check if Graph is a Tree

Check if Graph is a Tree

You are given an undirected graph with ( N ) vertices and ( M ) edges. The task is to determine whether the graph is a tree. In graph theory, a tree is a connected graph that contains no cycles, which is equivalent to having exactly ( N - 1 ) edges and being connected. If ( N = 0 ), the graph is not considered a tree. The input consists of a list of edges, and you need to verify if these conditions are met.

inputFormat

The first line contains two integers ( N ) and ( M ), representing the number of vertices and edges, respectively. Each of the following ( M ) lines contains two integers ( u ) and ( v ), indicating an undirected edge between vertices ( u ) and ( v ).

outputFormat

Output a single line containing "YES" if the graph is a tree; otherwise, output "NO".## sample

4 3
1 2
1 3
1 4
YES