luogu#P7951. [✗✓OI R1] 右方之火

[✗✓OI R1] 右方之火

Background

"A mere two billion, a mere two thousand years, is simply not enough."
"The power I have is far more than you think."

You cannot defeat the Fire of the Right.
But to prove that you have a rich imagination, you need to solve a constructive problem.

Problem Description

You are given a connected undirected graph with nn vertices and mm edges. Each vertex has an initial value aia_i and a target value bib_i. You may perform the following operation on this graph:

  • Choose an integer cc such that c1018|c| \le 10^{18}.
  • Choose a path with 33 vertices and 22 edges (a chain of length 22).
  • Decrease the value of the middle vertex by 2c2c, and increase the values of the other two vertices by cc each.

Determine whether it is possible to perform some operations so that finally every vertex value becomes bib_i. If it is possible, you need to output a sequence of operations, and the number of operations must not exceed 4n4n.

Note that cc can be negative.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, the number of test cases.
For each test case, the first line contains two positive integers n,mn,m, representing the number of vertices and edges.
The next two lines each contain nn integers, representing the initial values aia_i and the target values bib_i.
The next mm lines each contain two integers u,vu,v, meaning there is an edge between vertex uu and vertex vv. The graph is guaranteed to be connected, with no self-loops or multiple edges.

Output Format

For each test case, output a string in the first line. If it is possible to change the initial values into the target values, output Yes; otherwise output No. Do not print any extra characters on the line containing Yes or No, otherwise the checker may judge it as wrong.
In the second line, output an integer k(k4n)k(k\le4n), meaning the constructed plan has kk steps.
Then output kk lines. Each line contains four integers x,y,z,cx,y,z,c satisfying 1x,y,zn1 \le x,y,z \le n, c1018|c| \le 10^{18}, (x,y),(y,z)E(x,y),(y,z)\in E, meaning axax+c,ayay2c,azaz+ca_x \gets a_x+c,a_y \gets a_y-2c,a_z \gets a_z+c.

This problem uses Special Judge. If there are multiple valid answers, output any one.

3
7 6
8 6 0 6 1 1 10 
5 6 2 9 1 2 7 
1 2
2 3
3 4
4 5
5 6
6 7
7 6
8 6 0 6 1 0 11 
5 6 2 9 1 2 7 
1 2
2 3
3 4
4 5
5 6
6 7
7 6
8 6 0 6 1 1 10 
5 6 2 9 1 2 6 
1 2
2 3
3 4
4 5
5 6
6 7
Yes
5
7 6 5 -3
6 5 4 -5
5 4 3 -7
4 3 2 -6
3 2 1 -3
No
No

2
6 6
10 -3 4 21 -2 11 
4 4 8 8 8 9 
1 2
2 3
3 4
4 5
5 6
6 1
6 6
10 -3 4 21 -2 11 
4 4 8 8 9 8 
1 2
2 3
3 4
4 5
5 6
6 1
Yes
6
6 1 2 6
1 2 3 1
2 3 4 3
3 4 5 9
4 5 6 2
5 6 1 5
No

1
6 9
7 5 68 16 2 45
42 11 42 17 14 17
1 3
1 4
1 6
1 5
1 2
2 5
2 3
4 6
5 6
Yes
10
1 4 6 1
1 6 4 -6
2 1 4 -9
1 3 2 -17
5 1 4 25
2 1 6 32
1 6 5 33
4 1 3 39
4 6 5 -46
3 1 6 -99

Hint

This problem uses multiple test cases and subtask-based judging.

For 100%100\% of the data, 1T5×1041\le T \leq 5 \times 10^4, 3n5×1053 \le n \le 5 \times 10^5, n1m5×105n-1 \le m \le 5 \times 10^5, n,m5×105\sum n,\sum m \leq 5\times 10^5, ai,bi107|a_i|,|b_i| \leq 10^7.

subtask n,m,Tn,m,T Special Properties Score
1 n,m20n,m\le 20T5T \le 5 None 10
2 m=n1m=n-1 The ii-th edge connects vertices ii and (i+1)(i+1) 5
3 The ii-th edge connects vertices 11 and (i+1)(i+1)
4 None 10
5 m=nm=n The graph is guaranteed to be a cycle
6 n,m200,T5n,m\le 200,T \le 5 None
7 n,m2000,T5 n,m\le 2000,T \le 5 20
8 30

Friendly reminder: for some test points, T5T \le 5, so the testdata is relatively hard to construct. If you only get WA on a few points, it does not mean your algorithm is wrong. Please think carefully about the correctness of your algorithm.

Translated by ChatGPT 5