A String Problem

시간 제한2초메모리 제한2048 MB

요약
원 위 2N개 핀을 짝지은 N개의 현이 주어질 때, 모든 현이 평행하도록 만드는 최소 이동 횟수와 이동 순서를 구한다.
난이도

어려움10점 중 8점

유형
그리디, 구현, 수학, 정렬
정답자
아직 제출이 없습니다

문제

Lara loves flea markets. Last Saturday, there was the Rheinaue-Flohmarkt in Bonn, one of the biggest flea markets in Germany. Of course, Lara spent the whole day there, strolling through the market, haggling over prices, and buying all kinds of curious things. The most interesting thing she brought home was a small harp in a perfectly circular shape. When she wanted to start playing it, she noticed that the strings were all over the place rather than being parallel to each other.

More specifically, there are 2⋅N2\cdot N pins distributed evenly around the circular frame. Each of the NN strings is held in place by two of the pins, and every pin has exactly one string attached.

Lara does not know much about harps, but she strongly suspects that the strings should be aligned so that they are parallel to each other. To fix this issue, she decides to restring the harp. In each step, she can detach one end of a string from its pin and reattach it to a different pin. During the process it is okay if the ends of multiple strings are attached to the same pin. In the end, there should be exactly one string attached to every pin once again, and the NN strings should be parallel to each other.

Below you can find two examples of harps with parallel strings.

Since each step of restringing is a lot of work, Lara wants to restring the harp with as few steps as possible. Help Lara find a restringing sequence that takes the minimum number of steps!

입력

The first line of input contains one integer NN, denoting the number of strings. The strings are numbered from 00 to N−1N - 1.

Then follow NN lines, where the iith line (0≤i≤N−10\le i \le N-1) contains two integers a_ia\_i and b_ib\_i, the two pins that hold the iith string in place. The pins are numbered in clockwise order from 00 to 2⋅N−12\cdot N - 1. Every pin has exactly one string attached.

출력

Output an integer KK, the minimum number of steps needed to restring the harp such that all strings are parallel to each other.

Further, output KK lines, each containing three integers pp, ss, and ee, denoting that in this step of your solution, one end of the ppth string should be detached from pin ss and reattached to pin ee (0≤p≤N−10 \leq p \leq N - 1, 0≤s,e≤2⋅N−10 \leq s, e \leq 2\cdot N - 1).

Note that if the ppth string is not attached to pin ss at that moment, the sequence of moves is considered to be incorrect.

If several answers exist, you may print any of them. Note that partially correct answers may still score some points, as explained in the next section.

제한

  • 4≤N≤100,0004 \leq N \leq 100\\,000.
  • 0≤a_i,b_i≤2⋅N−10 \leq a\_i, b\_i \leq 2\cdot N - 1.
  • All a_ia\_i and b_ib\_i are unique.

예제4

  1. 예제 1

    입력
    5
    1 5
    4 9
    6 3
    2 7
    0 8
    
    예상 출력
    3
    4 8 9
    0 5 8
    1 9 5
    
  2. 예제 2

    입력
    5
    0 1
    3 2
    4 5
    6 7
    9 8
    
    예상 출력
    4
    1 3 9
    4 9 3
    2 5 7
    3 7 5
    
  3. 예제 3

    입력
    4
    1 4
    6 3
    5 2
    7 0
    
    예상 출력
    2
    0 4 6
    1 6 4
    
  4. 예제 4

    입력
    6
    3 9
    7 5
    10 2
    0 6
    1 11
    8 4
    
    예상 출력
    6
    3 6 1
    4 1 2
    2 2 3
    0 3 4
    5 4 5
    1 5 6