310979: CF1916F. Group Division

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

F. Group Divisiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

In the $31$st lyceum, there were two groups of olympiad participants: computer science and mathematics. The number of computer scientists was $n_1$, and the number of mathematicians was $n_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)$ are acquainted in the following case: there are people $a_1, a_2, \ldots,a_n$ ($1 \le a_i \le n_1 + n_2$) such that the following conditions are simultaneously met:

$\bullet$ Person $x$ is directly acquainted with $a_1$.

$\bullet$ Person $a_n$ is directly acquainted with $y$.

$\bullet$ Person $a_i$ is directly acquainted with $a_{i + 1}$ for any ($1 \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 $n_1$ people in the computer science group, and $n_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.

Input

Each test consists of several test cases. The first line contains a single integer $t$ ($1 \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 $n_1$, $n_2$, and $m$ ($1 \le n_1, n_2 \le 2000$, $1 \le m \le 5000$). $n_1$, $n_2$ are the sizes of the two groups described in the problem, and $m$ is the number of friendly connections initially.

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

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

It is guaranteed that the sum of $n_1 + n_2$ for all test cases does not exceed $2000$, and the sum of $m$ for all test cases does not exceed $5000$.

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

If there are several answers, print any of them.

Output

For each test case, output two lines.

In the first line, output $n_1$ distinct numbers $a_i$ ($1 \le a_i \le n_1 + n_2$) — the people belonging to the first group.

In the second line, output $n_2$ distinct numbers $b_i$ ($1 \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.

ExampleInput
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
Output
3 
1 2 
5 
1 2 3 4 
4 5 6 
1 2 3 
Note

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)$ are directly acquainted.

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

Consider all pairs of mathematicians and how they are acquainted:

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

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

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.

Output

**题目大意:**

在31中学里,有两个奥林匹克竞赛小组:计算机科学和数学。计算机科学小组有n1个人,数学小组有n2个人。不清楚具体谁属于哪个小组,但是知道某些人之间有友好关系(这些关系可能存在于同一小组内,也可能在不同小组之间)。

这些关系非常牢固,即使移除某个人及其所有友好关系,其他任何两个人之间仍然可以通过直接或共同的朋友保持联系。

教师们对计算机科学家和数学家之间的友谊感到不满,因此他们决定将学生分成两个小组,以使得满足以下两个条件:

- 计算机科学小组有n1个人,数学小组有n2个人。
- 任何两个计算机科学家之间都应相互认识(允许通过属于同一小组的共同朋友认识),数学家之间也是如此。

帮助他们解决这个问题,找出谁属于哪个小组。

**输入数据格式:**

每个测试包含多个测试用例。第一行包含一个整数t(1 ≤ t ≤ 1000)——测试用例的数量。接下来是测试用例的描述。

每个测试用例的第一行包含三个整数n1、n2和m(1 ≤ n1, n2 ≤ 2000,1 ≤ m ≤ 5000)。n1和n2是题目中描述的两个小组的大小,m是初始的友好关系数量。

接下来的m行描述友好关系:在第i行(1 ≤ i ≤ m)中,给出一个数字对(a, b),这意味着编号为a的人与编号为b的人是朋友(反之亦然)。

保证每个测试用例中所有的友好关系都是唯一的。

保证所有测试用例的n1 + n2之和不超过2000,m之和不超过5000。

还保证每个测试用例都有解。

如果有多个答案,可以输出其中任何一个。

**输出数据格式:**

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

第一行输出n1个不同的数字ai(1 ≤ ai ≤ n1 + n2)——属于第一组的人。

第二行输出n2个不同的数字bi(1 ≤ bi ≤ n1 + n2)——属于第二组的人。

所有数字必须是不同的。

如果有多个可能的答案,可以输出其中任何一个。**题目大意:** 在31中学里,有两个奥林匹克竞赛小组:计算机科学和数学。计算机科学小组有n1个人,数学小组有n2个人。不清楚具体谁属于哪个小组,但是知道某些人之间有友好关系(这些关系可能存在于同一小组内,也可能在不同小组之间)。 这些关系非常牢固,即使移除某个人及其所有友好关系,其他任何两个人之间仍然可以通过直接或共同的朋友保持联系。 教师们对计算机科学家和数学家之间的友谊感到不满,因此他们决定将学生分成两个小组,以使得满足以下两个条件: - 计算机科学小组有n1个人,数学小组有n2个人。 - 任何两个计算机科学家之间都应相互认识(允许通过属于同一小组的共同朋友认识),数学家之间也是如此。 帮助他们解决这个问题,找出谁属于哪个小组。 **输入数据格式:** 每个测试包含多个测试用例。第一行包含一个整数t(1 ≤ t ≤ 1000)——测试用例的数量。接下来是测试用例的描述。 每个测试用例的第一行包含三个整数n1、n2和m(1 ≤ n1, n2 ≤ 2000,1 ≤ m ≤ 5000)。n1和n2是题目中描述的两个小组的大小,m是初始的友好关系数量。 接下来的m行描述友好关系:在第i行(1 ≤ i ≤ m)中,给出一个数字对(a, b),这意味着编号为a的人与编号为b的人是朋友(反之亦然)。 保证每个测试用例中所有的友好关系都是唯一的。 保证所有测试用例的n1 + n2之和不超过2000,m之和不超过5000。 还保证每个测试用例都有解。 如果有多个答案,可以输出其中任何一个。 **输出数据格式:** 对于每个测试用例,输出两行。 第一行输出n1个不同的数字ai(1 ≤ ai ≤ n1 + n2)——属于第一组的人。 第二行输出n2个不同的数字bi(1 ≤ bi ≤ n1 + n2)——属于第二组的人。 所有数字必须是不同的。 如果有多个可能的答案,可以输出其中任何一个。

加入题单

上一题 下一题 算法标签: