#P9841. Yae Miko's Puzzle Reset
Yae Miko's Puzzle Reset
Yae Miko's Puzzle Reset
Yae Miko faces a puzzle‐reset problem. She has two weighted undirected complete graphs G and H, each with n vertices labeled 1 through n. The weight on edge (i,j) in graph G is Gij and in H is Hij (for 1 ≤ i < j ≤ n). To reset G into H she is allowed to perform the following operation as many times as she wishes:
- Select four distinct vertices a, b, c, d and an integer x.
- Increase the weights on edges (a, b), (a, c) and (a, d) by x.
- Decrease the weights on edges (b, c), (b, d) and (c, d) by x.
The effect of each operation is to add a vector v (with its six coordinates) to the edge weight differences. It can be shown that every such operation preserves the total sum of all edge differences (i.e. \( \sum_{1\le i
[ H_{ij}-G_{ij} = (H_{1i}-G_{1i}) + (H_{1j}-G_{1j}). ]
In other words, if we define for every vertex i (2 ≤ i ≤ n) the value
[ f_i = H_{1i}-G_{1i}, ]
then a necessary and sufficient condition for the existence of a sequence of operations (each of which changes the edge weights by a fixed pattern) converting G into H is that:
- \(\sum_{i=2}^n f_i = 0\); and
- for all 2 ≤ i < j ≤ n, \(H_{ij}-G_{ij} = f_i+f_j\).
Your task is to determine whether Yae can change G into H using the allowed operation. If yes, you should output a valid sequence of detailed steps. Otherwise, output NO
.
Note: In this problem, if G already equals H, then no operation is needed.
inputFormat
The input begins with an integer n (n \(\ge\) 4) denoting the number of vertices.
Then follow n lines, each containing n integers. The j-th integer on the i-th line denotes Gij (for 1 ≤ i, j ≤ n). It is guaranteed that G is symmetric and the diagonal entries (i.e. Gii) can be ignored.
After that, there are n lines each containing n integers describing the matrix H in the same format.
outputFormat
If it is impossible to transform G into H using the allowed operations, output a single line containing NO
(without quotes).
Otherwise, output YES
on the first line. On the second line, output an integer m (m \(\ge\) 0) denoting the number of operations. Then output m lines, each line containing five space‐separated integers: a, b, c, d and x, meaning that in that operation you chose vertices a, b, c, d and the integer x.
Important: The test data are such that if a solution exists then there is one meeting the invariants:
- \( \sum_{1\le i<j\le n} (H_{ij}-G_{ij}) = 0 \), and
- for all 2 ≤ i < j ≤ n, \( H_{ij}-G_{ij} = (H_{1i}-G_{1i}) + (H_{1j}-G_{1j}) \).
If G equals H, you may output 0 operations.
sample
4
0 1 2 3
1 0 3 2
2 3 0 1
3 2 1 0
0 1 2 3
1 0 3 2
2 3 0 1
3 2 1 0
YES
0
</p>