Baltic Olympiad in Informatics: 2013 Day 1, Problem 3
The city of Hotham is once again attacked by its most prominent villain, the Jester. This time his target is Hotham's water supply. The fresh water of Hotham is stored in
The Jester has breached some of the pipes and has been draining water from them. Following his playful nature, the Jester ensured that water drained from any one pipe amounts to an even number of cubic meters per second (
To make matters more confusing, the Jester actually pumps water into some of the breached pipes instead of draining from them. Again, the water pumped into any one pipe is an even number of
The mayor of Hotham has installed sensors in the reservoirs, but not in the pipes. Therefore, he can observe the net change of water in each reservoir but does not how much water is drained from or pumped into each pipe.
Your task is to write a program that helps the mayor. Given full information about the reservoir network and the net changes in each reservoir, your program should decide if this information is enough to uniquely determine the Jester's plan. The plan can be determined uniquely if there is exactly one possibility for how much water is drained from or pumped into each pipe. Note that these amounts of water need not be the same for all pipes. If there is exactly one possibility, your program should print it.
Constraints
If the Jester's plan can be determined uniquely,
Subtask 1 [30%]
The water network of Hotham is a tree.
Subtask 2 [70%]
No additional constraints.
Input Specification
The first line of the input contains two space-separated integers:
The following
The following
The input always describes a set of reservoir changes that can be realized by the Jester.
Output Specification
If the Jester's plan cannot be determined uniquely, your program should output a single line containing
If the Jester pumps
Sample Input 1
4 3
-1
1
-3
1
1 2
1 3
1 4
Sample Output 1
2
-6
2
Sample Input 2
4 5
1
2
1
2
1 2
2 3
3 4
4 1
1 3
Sample Output 2
0
Comments