#P15775. [JAG 2025 Summer Camp #2] Hanako' s Art II

[JAG 2025 Summer Camp #2] Hanako' s Art II

题目描述

There are 2n2n points in the xyxy-plane. Any two points have neither the same xx-coordinate nor the same yy-coordinate. Each point has a color represented by an integer between 11 and nn (inclusive). For each of the nn colors, there are exactly two points of that color.

An artist, Hanako, is willing to create a masterpiece by drawing nn polygonal chains in the xyxy-plane. According to her aesthetic sense, a masterpiece must satisfy all the following conditions.

  • Any two points having the same color are the endpoints of one of the polygonal chains.
  • Each polygonal chain consists of exactly two line segments, each of which is parallel to the xx- or yy-axis.
  • No two polygonal chains intersect.

Your task is to determine whether Hanako can create such a masterpiece.

输入格式

The input consists of multiple test cases. The first line of input contains an integer tt (1t500001 \leq t \leq 50\,000) representing the number of test cases. After that, tt test cases follow. Each of them is given in the following format.

$$\begin{aligned} & n \\ & y_1 \ c_1 \\ & \vdots \\ & y_{2n} \ c_{2n} \end{aligned} $$

The first line contains an integer nn (1n10001 \leq n \leq 1\,000) representing the number of polygonal chains which Hanako has to draw. Each of the following 2n2n lines contains two integers yiy_i and cic_i satisfying 1yi2n1 \leq y_i \leq 2n and 1cin1 \leq c_i \leq n. Each line represents that the ii-th point has the coordinate (i,yi)(i, y_i) and the color cic_i.

It is guaranteed that yiyjy_i \neq y_j if iji \neq j. In addition, no three points have the same color.

The sum of n2n^2 over all the test cases does not exceed 10610^6.

输出格式

If Hanako can create a masterpiece, print "Yes"; otherwise, print "No".

2
3
2 1
1 2
4 3
6 1
3 3
5 2
3
2 3
6 1
5 2
1 1
4 3
3 2
Yes
No

提示

One of the possible masterpieces in Sample Input 1 is depicted in Figure J-1.

:::align{center}

Figure J-1: Illustration of Sample Input 1 :::