Stake Coordinate Reconstruction

Time limit1sMemory limit128 MB

Summary
Given squared side lengths of triangles connecting numbered stakes in counter-clockwise order, reconstruct integer coordinates of every stake, with the first three fixed.
Level

Hard8 of 10

Topics
Graph, Geometry, BFS, Implementation
Solved
No attempts yet

Problem

Stakes have been driven into a grassy field where a new math building will go, and you must reconstruct their coordinates.

Surveying shows that the first three stakes form a right-angled triangle whose legs are each 11 metre long and whose hypotenuse is 2\sqrt{2} metres long. These three stakes are placed at coordinates (0,0)(0,0), (0,1)(0,1), and (1,0)(1,0); that is, stakes 11, 22, and 33 are at (0,0)(0,0), (0,1)(0,1), and (1,0)(1,0) respectively. All of the other stakes also land exactly on lattice points (points with integer coordinates).

Given the triangle measurements, determine the coordinates of every stake.

Input

The input consists of several test cases. The first line of each test case contains two integers nn and mm, each at least 11 and at most 10001000. The integer nn is the number of lines that follow, and mm is the number of stakes. The stakes are numbered 11 to mm, and all stakes are at distinct locations. Each stake's xx and yy coordinates are between −1000000-1000000 and 10000001000000 inclusive.

Each of the following nn lines contains exactly six integers aa, bb, cc, xx, yy, zz. The integers aa, bb, cc are the numbers of three stakes, always listed in counter-clockwise order: moving from stake aa to stake bb to stake cc, you turn left at stake bb. The value xx is the square of the distance from stake aa to stake bb, yy is the square of the distance from stake bb to stake cc, and zz is the square of the distance from stake cc to stake aa.

Every stake appears in at least one line. Moreover, for every pair of stakes aa, bb there is a subset of the triangles forming a sequence T1,T2,…,TnT_1, T_2, \ldots, T_n such that TiT_i and Ti+1T_{i+1} share two vertices for all 0<i<n0 < i < n, with aa a vertex of T1T_1 and bb a vertex of TnT_n. The last line of input is 0 00\ 0; these zeros are not values of nn and mm and must not be processed as such.

Output

For each test case, output exactly mm lines describing stakes 11 to mm in order. Each line contains two integers, the xx and yy coordinates of that stake, separated by a space. The first three lines of output for each test case are always:

0 0
0 1
1 0

Examples2

  1. Example 1

    Input
    1 3
    1 3 2 1 2 1
    0 0
    
    Expected output
    0 0
    0 1
    1 0
    
  2. Example 2

    Input
    2 4
    1 3 2 1 2 1
    2 3 4 2 1 1
    0 0
    
    Expected output
    0 0
    0 1
    1 0
    1 1