#K42897. Determine if a Graph is a Tree
Determine if a Graph is a Tree
Determine if a Graph is a Tree
You are given an undirected graph with n vertices and m edges. A graph is considered a tree if it meets the following criteria:
- The graph is connected.
- The graph contains exactly \( n-1 \) edges.
- The graph does not contain any cycles.
Your task is to determine whether the given graph is a tree. In other words, check if the input graph is connected and has no cycles while having exactly \( n-1 \) edges.
inputFormat
The input begins with an integer T denoting the number of test cases. Each test case starts with a line containing two integers n and m — the number of vertices and the number of edges, respectively. The next m lines each contain two integers representing an undirected edge between two vertices.
outputFormat
For each test case, output a single line: "YES" if the provided graph is a tree, or "NO" otherwise.
## sample1
4 3
1 2
2 3
3 4
YES
</p>