CF1702E.Split Into Two Sets

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Polycarp was recently given a set of nn (number nn — even) dominoes. Each domino contains two integers from 11 to nn.

Can he divide all the dominoes into two sets so that all the numbers on the dominoes of each set are different? Each domino must go into exactly one of the two sets.

For example, if he has 44 dominoes: 1,4{1, 4}, 1,3{1, 3}, 3,2{3, 2} and 4,2{4, 2}, then Polycarp will be able to divide them into two sets in the required way. The first set can include the first and third dominoes (1,4{1, 4} and 3,2{3, 2}), and the second set — the second and fourth ones (1,3{1, 3} and 4,2{4, 2}).

波利卡普最近得到了一组 nn(nn 为偶数)个骨牌。每个骨牌包含两个取值范围为 11 到 nn 的整数。

他能否将所有骨牌划分为两个集合,使得每个集合中所有骨牌上的数字互不相同?每个骨牌必须且仅能放入这两个集合中的一个。

例如,若他有 44 个骨牌:1,4{1, 4}、1,3{1, 3}、3,2{3, 2} 和 4,2{4, 2},则波利卡普能够以满足要求的方式将它们划分为两个集合。第一个集合可包含第一和第三个骨牌(1,4{1, 4} 和 3,2{3, 2}),第二个集合包含第二和第四个骨牌(1,3{1, 3} 和 4,2{4, 2})。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains a single even integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the number of dominoes.

The next nn lines contain pairs of numbers aia_i and bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n) describing the numbers on the ii-th domino.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

接下来是各测试用例的描述。

每个测试用例的第一行包含一个偶数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 多米诺骨牌的数量。

接下来的 nn 行每行包含两个数 aia_i 和 bib_i(1≤ai,bi≤n1 \le a_i, b_i \le n),表示第 ii 张多米诺骨牌上的两个数字。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case print:

  • YES, if it is possible to divide nn dominoes into two sets so that the numbers on the dominoes of each set are different;
  • NO if this is not possible.

You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer).

对每个测试用例,输出:

  • YES,如果可以将 nn 个骨牌分成两组,使得每组中所有骨牌上的数字互不相同;
  • NO,如果无法做到这一点。

YES 和 NO 的大小写不限(例如,字符串 yEs、yes、Yes 和 YES 均被视为肯定回答)。

输入输出样例

  • 输入#1

    6
    4
    1 2
    4 3
    2 1
    3 4
    6
    1 2
    4 5
    1 3
    4 6
    2 3
    5 6
    2
    1 1
    2 2
    2
    1 2
    2 1
    8
    2 1
    1 2
    4 3
    4 3
    5 6
    5 7
    8 6
    7 8
    8
    1 2
    2 1
    4 3
    5 3
    5 4
    6 7
    8 6
    7 8

    输出#1

    YES
    NO
    NO
    YES
    YES
    NO

说明/提示

In the first test case, the dominoes can be divided as follows:

  • First set of dominoes: [1,2,4,3][{1, 2}, {4, 3}]
  • Second set of dominoes: [2,1,3,4][{2, 1}, {3, 4}]

In other words, in the first set we take dominoes with numbers 11 and 22, and in the second set we take dominoes with numbers 33 and 44.

In the second test case, there's no way to divide dominoes into 22 sets, at least one of them will contain repeated number.

在第一个测试用例中,多米诺骨牌可以按如下方式划分:

  • 第一组多米诺骨牌:[1,2,4,3][{1, 2}, {4, 3}]
  • 第二组多米诺骨牌:[2,1,3,4][{2, 1}, {3, 4}]

换言之,在第一组中我们选取编号为 11 和 22 的多米诺骨牌,而在第二组中我们选取编号为 33 和 44 的多米诺骨牌。

在第二个测试用例中,无法将多米诺骨牌划分为 22 组,至少有一组会包含重复的数字。

输入解题思路,AI测评打分。不知道怎么写?

首页