#P5959. [POI 2018] Plan metra

[POI 2018] Plan metra

Problem Description

There is an unrooted tree with nn nodes. Each edge has a positive integer weight representing its length. The distance between two nodes is defined as the length of the shortest path between them in the tree.

You are given, for every node from 22 to n1n-1, its distance to node 11 and its distance to node nn in the tree. Reconstruct the tree from this information.

Input Format

The first line contains a positive integer nn, representing the number of nodes.

The second line contains n2n-2 positive integers d(1,2),d(1,3),...,d(1,n1)d(1,2),d(1,3),...,d(1,n-1), representing the distance from each node to node 11.

The third line contains n2n-2 positive integers d(n,2),d(n,3),...,d(n,n1)d(n,2),d(n,3),...,d(n,n-1), representing the distance from each node to node nn.

Output Format

If there is no solution, output NIE.

Otherwise, output TAK on the first line. Then output n1n-1 lines, each containing three positive integers u,v,cu,v,c, indicating that there is a tree edge connecting nodes uu and vv with length cc.

If there are multiple solutions, output any one of them.

This problem uses a Special Judge.

7
6 6 2 2 1
5 3 5 1 4
TAK
1 5 2
5 7 1
5 2 4
7 3 3
1 4 2
1 6 1

Hint

For 100%100\% of the testdata, 2n5000002\le n\le 500000, 1d10000001\le d\le 1000000, 1u,vn1\le u,v\le n, 1c10000001\le c\le1000000.

Translated by ChatGPT 5