CF1916F.Group Division

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

In the 3131st lyceum, there were two groups of olympiad participants: computer science and mathematics. The number of computer scientists was n1n_1, and the number of mathematicians was n2n_2. It is not known for certain who belonged to which group, but it is known that there were friendly connections between some pairs of people (these connections could exist between a pair of people from the same group or from different groups).

The connections were so strong that even if one person is removed along with all their friendly connections, any pair of people still remains acquainted either directly or through mutual friends.

†^{\dagger} More formally, two people (x,y)(x, y) are acquainted in the following case: there are people a1,a2,…,ana_1, a_2, \ldots,a_n (1≤ai≤n1+n21 \le a_i \le n_1 + n_2) such that the following conditions are simultaneously met:

∙\bullet Person xx is directly acquainted with a1a_1.

∙\bullet Person ana_n is directly acquainted with yy.

∙\bullet Person aia_i is directly acquainted with ai+1a_{i + 1} for any (1≤i≤n−11 \le i \le n - 1).

The teachers were dissatisfied with the fact that computer scientists were friends with mathematicians and vice versa, so they decided to divide the students into two groups in such a way that the following two conditions are met:

∙\bullet There were n1n_1 people in the computer science group, and n2n_2 people in the mathematics group.

∙\bullet Any pair of computer scientists should be acquainted (acquaintance involving mutual friends, who must be from the same group as the people in the pair, is allowed), the same should be true for mathematicians.

Help them solve this problem and find out who belongs to which group.

在第31中学,有两组参加奥赛的学生:计算机科学组和数学组。计算机科学组的人数为 n1n_1,数学组的人数为 n2n_2。目前尚不能确定每个人具体属于哪一组,但已知某些人之间存在友谊关系(这些友谊关系可能存在于同一组内的两人之间,也可能存在于不同组的两人之间)。

这些友谊关系极为牢固:即使移除某一个人及其所有友谊连接,任意两人之间仍能通过直接相识或经由共同朋友间接相识而保持“相识”关系。

†^{\dagger} 更严格地定义,“相识”关系如下:对两个人 (x,y)(x, y),若存在一系列人 a1,a2,…,ana_1, a_2, \ldots, a_n(其中 1≤ai≤n1+n21 \le a_i \le n_1 + n_2),使得以下条件同时成立,则称 xx 与 yy 相识:

∙\bullet xx 与 a1a_1 直接相识;
∙\bullet ana_n 与 yy 直接相识;
∙\bullet 对任意 1≤i≤n−11 \le i \le n - 1,aia_i 与 ai+1a_{i + 1} 直接相识。

教师们对计算机科学组学生与数学组学生之间互为朋友的现象感到不满,因此决定将全体学生重新划分为两个组,满足以下两个条件:

∙\bullet 计算机科学组恰好有 n1n_1 人,数学组恰好有 n2n_2 人;
∙\bullet 任意两名计算机科学组学生之间必须相识(允许通过共同朋友间接相识,但这些共同朋友也必须同属计算机科学组);同理,任意两名数学组学生之间也必须相识(中间共同朋友也必须同属数学组)。

请帮助他们解决该问题,确定每个人应归属的组别。

输入格式

Each test consists of several test cases. The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains three integers n1n_1, n2n_2, and mm (1≤n1,n2≤20001 \le n_1, n_2 \le 2000, 1≤m≤50001 \le m \le 5000). n1n_1, n2n_2 are the sizes of the two groups described in the problem, and mm is the number of friendly connections initially.

The following mm lines describe the friendly connections: in the ii-th (1≤i≤m1 \le i \le m) line, a pair of numbers (a,b)(a, b) is given, which means that the person with number aa is friends with the person with number bb (and vice versa).

It is guaranteed that for each test case, all friendly connections are distinct.

It is guaranteed that the sum of n1+n2n_1 + n_2 for all test cases does not exceed 20002000, and the sum of mm for all test cases does not exceed 50005000.

It is also guaranteed that for each test case, a solution exists.

If there are several answers, print any of them.

每个测试包含若干个测试用例。第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含三个整数 n1n_1、n2n_2 和 mm(1≤n1,n2≤20001 \le n_1, n_2 \le 2000,1≤m≤50001 \le m \le 5000)。其中 n1n_1、n2n_2 分别为题目中所述两个组的大小,mm 为初始友好关系的数量。

接下来的 mm 行描述这些友好关系:第 ii 行(1≤i≤m1 \le i \le m)给出一对数字 (a,b)(a, b),表示编号为 aa 的人与编号为 bb 的人互为朋友(关系是双向的)。

保证每个测试用例中所有友好关系互不相同。

保证所有测试用例的 n1+n2n_1 + n_2 之和不超过 20002000,且所有测试用例的 mm 之和不超过 50005000。

同时保证每个测试用例均有解。

若存在多个答案,输出任意一个即可。

输出格式

For each test case, output two lines.

In the first line, output n1n_1 distinct numbers aia_i (1≤ai≤n1+n21 \le a_i \le n_1 + n_2) — the people belonging to the first group.

In the second line, output n2n_2 distinct numbers bib_i (1≤bi≤n1+n21 \le b_i \le n_1 + n_2) — the people belonging to the second group.

All numbers must be distinct.

If there are several possible answers, print any one.

对于每个测试用例,输出两行。

第一行输出 n1n_1 个互不相同的数 aia_i(1≤ai≤n1+n21 \le a_i \le n_1 + n_2)——属于第一组的人。

第二行输出 n2n_2 个互不相同的数 bib_i(1≤bi≤n1+n21 \le b_i \le n_1 + n_2)——属于第二组的人。

所有数字必须互不相同。

如果存在多种可能的答案,输出任意一个即可。

输入输出样例

  • 输入#1

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

    输出#1

    3 
    1 2 
    5 
    1 2 3 4 
    4 5 6 
    1 2 3

说明/提示

Consider the third test case. The division into groups looks as follows:

The students selected as computer scientists are colored in green, and those selected as mathematicians are colored in blue.

Consider all pairs of computer scientists and how they are acquainted:

Pairs (4,5),(4,6)(4, 5), (4, 6) are directly acquainted.

Pair (5,6)(5, 6) is acquainted through the computer scientist with number 44.

Consider all pairs of mathematicians and how they are acquainted:

Pairs (1,2),(2,3)(1, 2), (2, 3) are directly acquainted.

Pair (1,3)(1, 3) is acquainted through the mathematician with number 22.

We conclude that any pair of people belonging to the same group is acquainted with each other, thus the division into two groups is correct.

考虑第三个测试用例。分组情况如下所示:

被选为计算机科学家的学生以绿色标出,被选为数学家的学生以蓝色标出。

考虑所有计算机科学家之间的配对及其相识关系:

配对 (4,5)(4, 5)、(4,6)(4, 6) 是直接相识的。

配对 (5,6)(5, 6) 通过编号为 44 的计算机科学家而相识。

考虑所有数学家之间的配对及其相识关系:

配对 (1,2)(1, 2)、(2,3)(2, 3) 是直接相识的。

配对 (1,3)(1, 3) 通过编号为 22 的数学家而相识。

我们得出结论:同一组内的任意两人均彼此相识,因此划分为两个组是正确的。

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

首页