Pipes

Time limit1sMemory limit128 MB

Summary
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 NN reservoirs, which are connected by a set of MM 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 (m3/s\text{m}^3/\text{s}). If 2d m3/s2d\ \text{m}^3/\text{s} of water is drained from a pipe joining reservoirs uu and vv, then uu and vv lose d m3/sd\ \text{m}^3/\text{s} 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 m3/s\text{m}^3/\text{s}. If 2p m3/s2p\ \text{m}^3/\text{s} of water is pumped into a pipe joining reservoirs uu and vv, then uu and vv gain p m3/sp\ \text{m}^3/\text{s} 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 2d1,2d2,…,2da m3/s2d_1, 2d_2, \dots, 2d_a\ \text{m}^3/\text{s} of water is drained and to pipes into which 2p1,2p2,…,2pb m3/s2p_1, 2p_2, \dots, 2p_b\ \text{m}^3/\text{s} of water is pumped, then the net change of water volume in this reservoir is p1+p2+⋯+pb−d1−d2−⋯−dap_1 + p_2 + \dots + p_b - d_1 - d_2 - \dots - d_a.

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: NN, the number of reservoirs in Hotham, and MM, the number of pipes. The following NN lines contain an integer cic_i each: the net change in reservoir ii (1≤i≤N1 \le i \le N). Line ii of these NN lines contains cic_i. The following MM lines contain two integers uiu_i and viv_i each (1≤ui,vi≤N1 \le u_i, v_i \le N). Each such line indicates that there is a pipe between reservoirs uiu_i and viv_i. Line ii of these MM lines contains uiu_i and viv_i.

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 00. Otherwise, your program should output MM lines with one integer xix_i each (1≤i≤M1 \le i \le M). Line ii should contain xix_i. If the Jester drains di m3/sd_i\ \text{m}^3/\text{s} of water from the pipe between uiu_i and viv_i, let xi=−dix_i = -d_i. If the Jester pumps pi m3/sp_i\ \text{m}^3/\text{s} of water into the pipe between uiu_i and viv_i, let xi=pix_i = p_i. If the Jester does not add or remove water from the pipe between uiu_i and viv_i, let xi=0x_i = 0.

Constraints

  • 1≤N≤1000001 \le N \le 100000
  • 1≤M≤5000001 \le M \le 500000
  • −109≤ci≤109-10^9 \le c_i \le 10^9
  • If the Jester's plan can be determined uniquely, −109≤xi≤109-10^9 \le x_i \le 10^9.

Examples2

  1. Example 1

    Input
    4 3
    -1
    1
    -3
    1
    1 2
    1 3
    1 4
    
    Expected output
    2
    -6
    2
    
  2. Example 2

    Input
    4 5
    1
    2
    1
    2
    1 2
    2 3
    3 4
    4 1
    1 3
    
    Expected output
    0