#P12826. 「DLESS-2」XOR and Tree Construction

    ID: 12376 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>Special JudgeO2优化Ad-hoc洛谷比赛

「DLESS-2」XOR and Tree Construction

Description

Bob has an unrooted tree TT with nn vertices. Each vertex has a weight, with the weight of vertex ii being aia_i. Alice has recorded an n×nn \times n matrix bb, where bi,jb_{i,j} is the XOR sum of the weights of all vertices on the shortest path from vertex ii to vertex jj.

Alice observed this matrix and, to her surprise, found that all its elements are positive integers.

Unfortunately, one day, Bob lost his tree. Now, you are given Alice's matrix bb, and your task is to reconstruct a possible tree TT and the sequence of weights aa. It is worth noting that Alice is very reliable, so it is guaranteed that at least one such tree can always be reconstructed.

Input Format

The input consists of multiple test cases. The first line contains a single positive integer TT, representing the number of test cases.

For each test case:

  • The first line contains a positive integer nn.
  • The next nn lines describe the matrix bb. The jj-th integer on the ii-th of these lines is bi,jb_{i,j}.

Output Format

For each test case:

  • In the first line, output nn integers, representing the vertex weights a1,a2,,ana_1, a_2, \ldots, a_n.
  • Then, output n1n-1 lines. Each line should contain two integers u,vu,v, representing an edge between vertex uu and vertex vv.
2
3
1 3 7
3 2 6
7 6 4
5
8 9 10 15 1 
9 1 2 14 9 
10 2 3 13 10 
15 14 13 7 6 
1 9 10 6 8 

1 2 4 
1 2
2 3
8 1 3 7 8 
1 2
1 4
2 3
2 5

1
2
10000000000 29999508480
29999508480 20000000000
10000000000 20000000000
1 2

Hint

For all test data, it is guaranteed that:

  • 1T101\le T\le 10
  • 1n5001\le n\le 500
  • 1bi,j<2601\le b_{i,j}<2^{60}

This problem uses bundled tests. The subtasks are described as follows:

  • Subtask 1 (5 pts): n=2n=2.
  • Subtask 2 (10 pts): n=3n=3.
  • Subtask 3 (10 pts): n=4n=4.
  • Subtask 4 (15 pts): n8n\le 8.
  • Subtask 5 (25 pts): The values bi,ib_{i,i} are all distinct.
  • Subtask 6 (35 pts): No additional constraints.