PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000228. Tấm bản đồ bị thất lạc (Lost Map)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tấm bản đồ bị thất lạc (Lost Map)

Lost Map

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn villages connected by exactly n−1n-1 undirected roads, forming a tree. Every road has a positive integer length. You lost the road map but retained the n×nn\times n distance table: di,jd_{i,j} is the total length along the unique route from village ii to village jj.

Recover the original n−1n-1 direct road connections. The roads may be printed in any order and with either endpoint first.

Input

  • The first line contains integer nn.
  • The next nn lines each contain nn integers. Entry di,jd_{i,j} is the distance between villages i,ji,j. For i≠ji\ne j, di,j=dj,i>0d_{i,j}=d_{j,i}>0, while di,i=0d_{i,i}=0. The matrix is guaranteed to be a tree-distance matrix of positive edge lengths.

Output

Print n−1n-1 lines, each containing u,vu,v for one original direct road. Print every road exactly once.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le 20; di,i=0d_{i,i}=0; 1≤di,j<107 (i≠j)1\le d_{i,j}<10^7\ (i\ne j); di,j=dj,id_{i,j}=d_{j,i}; ma trận là metric đường đi trên một cây cạnh dương.
  • Subtask 2 (30 points): 2≤n≤2502\le n\le 250; di,i=0d_{i,i}=0; 1≤di,j<107 (i≠j)1\le d_{i,j}<10^7\ (i\ne j); di,j=dj,id_{i,j}=d_{j,i}; ma trận là metric đường đi trên một cây cạnh dương.
  • Subtask 3 (50 points): 2≤n≤25002\le n\le 2500; di,i=0d_{i,i}=0; 1≤di,j<107 (i≠j)1\le d_{i,j}<10^7\ (i\ne j); di,j=dj,id_{i,j}=d_{j,i}; ma trận là metric đường đi trên một cây cạnh dương.

Examples

Input

4
0 1 1 2
1 0 2 3
1 2 0 3
2 3 3 0

Output

1 2
1 3
1 4

Explanation

Villages 22 and 33 each have distance 11 from village 11, giving direct roads 1−21-2 and 1−31-3. Village 44 is at distance 22 from village 11 but 33 from villages 22 and 33, so the direct road is 1−41-4 of length 22. Thus the three roads are (1,2),(1,3),(1,4)(1,2),(1,3),(1,4).