QOJ.ac

QOJ

时间限制: 1 s 内存限制: 256 MB 总分: 100

#2667. Challenging NPC

统计

Little N has recently been studying NP-complete problems. Seeing Little N working with great enthusiasm, Little O posed the following problem to him:

There are $n$ balls, numbered $1$ to $n$. There are also $m$ baskets, numbered $1$ to $m$. Each basket can hold at most $3$ balls.

Each ball can only be placed into specific baskets. Specifically, there are $e$ conditions, where the $i$-th condition is described by two integers $v_i$ and $u_i$, indicating that the ball numbered $v_i$ can be placed into the basket numbered $u_i$.

Every ball must be placed into a basket. If a basket contains no more than $1$ ball, we call such a basket "half-empty."

Find the maximum number of half-empty baskets, and in an optimal configuration, which basket each ball is placed into.

After seeing the problem, Little N was instantly stumped. Little I, who was watching from the side, chuckled and said, "Easy problem!" and proceeded to describe a polynomial-time algorithm in a few words.

Little N was stunned. Three seconds later, he came to his senses and slammed the table: "No! This problem is clearly NP-complete; your algorithm must be wrong!"

Little I smiled: "Well then, wait for me to receive the Turing Award!"

Little O only knows how to pose problems, not how to solve them, so he found you—please investigate this problem and write a program to solve it.

Input

The first line contains a single positive integer $T$, representing the number of test cases.

For each test case, the first line contains three positive integers $n, m, e$, representing the number of balls, the number of baskets, and the number of conditions, respectively.

The next $e$ lines each contain two integers $v_i, u_i$, indicating that the ball numbered $v_i$ can be placed into the basket numbered $u_i$.

Output

For each test case, first output a single line containing an integer, representing the maximum number of half-empty baskets.

Then, output another line containing $n$ integers $p_1, p_2, \dots, p_n$, separated by spaces, representing an optimal solution. Here, $p_i$ indicates that the ball numbered $i$ is placed into the basket numbered $p_i$. If there are multiple optimal solutions, you may output any one of them.

Examples

Input 1

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

Output 1

2
1 2 3 3

Input 2

See npc/npc.in and npc/npc.ans in the contestant directory.

Constraints

For all data, $T \le 5$, $1 \le n \le 3m$. It is guaranteed that $1 \le v_i \le n$, $1 \le u_i \le m$, and no duplicate conditions will appear.

It is guaranteed that there is at least one valid configuration where every ball is placed into a basket and each basket contains no more than $3$ balls.

The test cases satisfy the following conditions:

Test Case $m$ Constraints
1 $\le 10$ $n \le 20, e \le 25$
2
3 $\le 100$ $e = nm$
4 There exists a configuration with $m$ half-empty baskets
5 There is no configuration with half-empty baskets
6
7 None
8
9
10

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.