#C8070. Forest Balance Checker
Forest Balance Checker
Forest Balance Checker
In this problem, you are given a series of test cases describing a forest (an undirected graph that can be disconnected) in a simplified manner. For each test case, the forest is represented by the number of vertices N and the number of edges M, followed by M pairs of integers representing undirected edges between vertices.
Your task is to determine if the forest is balanced. A forest is considered balanced if every vertex has the same degree. The degree of a vertex is defined as the number of edges incident to it. Note that if there are no edges (i.e. the degree of every vertex is 0), the forest is balanced.
You can express the condition mathematically as follows:
$$\text{The forest is balanced} \iff \forall i, j \in \{1, 2, \dots, N\},\; d_i = d_j $$inputFormat
The first line of input contains an integer T denoting the number of test cases. Each test case is described as follows:
- The first line contains two integers N and M — the number of vertices and the number of edges.
- This is followed by M lines, each containing two integers u and v representing an undirected edge between vertices u and v.
outputFormat
For each test case, print a single line containing YES
if the forest is balanced or NO
otherwise.
1
3 3
1 2
2 3
3 1
YES
</p>