#C1910. Equalize Energy Levels
Equalize Energy Levels
Equalize Energy Levels
You are given several test cases. In each test case you have \(N\) space stations with initial energy levels and \(M\) connections between them. Two stations connected by a connection can freely exchange energy. This means that within any connected group (component) of stations, energy can be redistributed arbitrarily.
To determine if it is possible to equalize the energy among the stations in a connected component, compute the total energy in that component and check whether it is divisible by the number of stations in the component. In other words, let \(S\) be the sum of the energies and \(k\) be the number of stations in the component; equalization is possible if and only if \(S \mod k = 0\). Your task is to decide for each test case if all stations can have equal energy after redistribution. Print YES
if it is possible and NO
otherwise.
inputFormat
The input begins with 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), representing the number of space stations and the number of connections, respectively.
- The second line contains (N) integers, where each integer represents the initial energy level of a station.
- Each of the next (M) lines contains two integers (u) and (v), indicating a bidirectional connection between station (u) and station (v).
All input is provided via standard input (stdin).
outputFormat
For each test case, output a single line: print YES
if it is possible to equalize the energy levels in all connected components, or NO
otherwise. Output should be printed to standard output (stdout).## sample
6
4 2
1 2 3 4
1 2
3 4
3 3
1 3 5
1 2
2 3
3 1
3 2
2 2 2
1 2
2 3
5 2
1 1 1 2 1
1 2
4 5
4 6
1 2 3 4
1 2
2 3
3 4
1 3
1 4
2 4
3 3
3 3 3
1 2
2 3
3 1
NO
YES
YES
NO
NO
YES
</p>