아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Classical Maximization Problem

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

요약
서로 다른 격자점 2n개를 모두 짝지어 x좌표나 y좌표가 같은 짝의 수를 최대로 만들고, 그 개수와 짝 구성을 출력한다.
난이도

보통10점 중 7점

유형
그래프, 그리디, 구현, 해시맵
정답자
아직 제출이 없습니다

문제

You are given 2n2n distinct points on a plane. Point ii has integer coordinates (x_i,y_i)(x\_i, y\_i).

Points ii and jj are a friendly pair if either x_i=x_jx\_i = x\_j or y_i=y_jy\_i = y\_j.

Form nn pairs of points. Every point must belong to exactly one pair. The number of friendly pairs among your nn pairs must be maximized.

입력

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5).

The ii-th of the next 2n2n lines contains two integers x_ix\_i and y_iy\_i, denoting the coordinates of the ii-th point (−109≤x_i,y_i≤109-10^9 \le x\_i, y\_i \le 10^9). All points are distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

출력

For each test case, print a non-negative integer kk, denoting the maximum possible number of friendly pairs.

In the ii-th of the next nn lines, print two integers a_ia\_i and b_ib\_i, denoting a pair formed by points a_ia\_i and b_ib\_i (1≤a_i,b_i≤2n1 \le a\_i, b\_i \le 2n; a_i≠b_ia\_i \ne b\_i).

Every integer from 11 to 2n2n must appear among a_ia\_i and b_ib\_i exactly once. The number of indices ii such that points a_ia\_i and b_ib\_i are a friendly pair must be equal to kk.

예제1

  1. 예제 1

    입력
    3
    2
    0 0
    0 1
    1 0
    1 1
    2
    0 0
    0 1
    0 2
    0 3
    2
    0 0
    1 1
    2 2
    3 3
    
    예상 출력
    2
    2 4
    3 1
    2
    4 3
    2 1
    0
    1 2
    3 4