#P7951. Graph Weight Transformation

    ID: 21135 Type: Default 1000ms 256MiB

Graph Weight Transformation

Graph Weight Transformation

You are given a connected undirected graph with (n) vertices and (m) edges. Each vertex is assigned an initial weight (a_i) and a target weight (b_i). You are allowed to perform the following operation up to (4n) times:

  1. Choose an integer \(c\) satisfying \(|c| \le 10^{18}\).
  2. Select a chain of 3 distinct vertices (i.e. a simple path of length 2), say \(u, v, w\) such that \(u\) is adjacent to \(v\) and \(v\) is adjacent to \(w\).
  3. Update the weights as follows: \[ a_u \leftarrow a_u + c, \quad a_v \leftarrow a_v - 2c, \quad a_w \leftarrow a_w + c \]

Determine whether it is possible to transform the initial weights into the target weights using a sequence of the above operations. If it is possible, you must output a valid sequence of operations (with at most (4n) operations). Otherwise, output “NO”.

Note: (c) may be negative. All formulas are in (\LaTeX) format.

inputFormat

The first line contains two integers (n) and (m).

The second line contains (n) integers (a_1, a_2, \dots, a_n), which are the initial weights.

The third line contains (n) integers (b_1, b_2, \dots, b_n), which are the target weights.

Each of the next (m) lines contains two integers (u) and (v), representing an edge between vertices (u) and (v). It is guaranteed that the graph is connected.

outputFormat

If the transformation is possible, print “YES” on the first line, followed by an integer (k) (with (0 \le k \le 4n)), representing the number of operations used. Then print (k) lines, each containing four space‐separated values: (u, v, w, c), where the operation is applied on chain (u)–(v)–(w) with the chosen integer (c).

If it is not possible, simply print “NO”.

sample

3 3
1 2 3
1 2 3
1 2
2 3
3 1
YES

0

</p>