Pipes
Time limit1sMemory limit128 MB
Given net volume changes at each vertex of a connected graph, decide whether the flow on every edge is uniquely determined and print those flows if so.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Math, Implementation
- Solved
- No attempts yet
Problem
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 reservoirs, which are connected by a set of pipes. There is at least one path (potentially consisting of several pipes) from any reservoir to any other reservoir. Moreover, every pipe connects two different reservoirs, and there is at most one pipe between any pair of reservoirs.
The Jester has breached some of the pipes and has been draining water from them. Following his playful nature, the Jester ensured that the water drained from any one pipe amounts to an even number of cubic meters per second (). If of water is drained from a pipe joining reservoirs and , then and lose of water each.
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 . If of water is pumped into a pipe joining reservoirs and , then and gain of water each. The net change of water volume in each reservoir is the total sum of gains and losses acquired from the pipes connected to it. Formally, if a reservoir is connected to pipes from which of water is drained and to pipes into which of water is pumped, then the net change of water volume in this reservoir is .
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 know 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.
Input
The first line of the input contains two integers: , the number of reservoirs in Hotham, and , the number of pipes. The following lines contain an integer each: the net change in reservoir (). Line of these lines contains . The following lines contain two integers and each (). Each such line indicates that there is a pipe between reservoirs and . Line of these lines contains and .
The input always describes a set of reservoir changes that can be realized by the Jester.
Output
If the Jester's plan cannot be determined uniquely, your program should output a single line containing . Otherwise, your program should output lines with one integer each (). Line should contain . If the Jester drains of water from the pipe between and , let . If the Jester pumps of water into the pipe between and , let . If the Jester does not add or remove water from the pipe between and , let .
Constraints
- If the Jester's plan can be determined uniquely, .