#K40822. Determine if a Graph is a Tree

    ID: 26728 Type: Default 1000ms 256MiB

Determine if a Graph is a Tree

Determine if a Graph is a Tree

You are given an undirected graph with n nodes and m edges. Your task is to determine whether the given graph forms a tree. A graph is a tree if it is connected and has exactly n - 1 edges.

Recall that a tree must satisfy the following conditions:

  • The number of edges is exactly n - 1.
  • The graph is connected; that is, there is a path between any two nodes.

If both conditions are met, output YES; otherwise, output NO.

The input is given via standard input and the output should be written to standard output.

Note: Ensure your solution correctly handles edge cases such as a single node graph.

inputFormat

The first line contains two integers n and m, representing the number of nodes and edges respectively. The following m lines each contain two integers u and v indicating there is an undirected edge between nodes u and v.

outputFormat

Output a single line containing either 'YES' if the graph is a tree or 'NO' otherwise.## sample

4 3
1 2
2 3
3 4
YES