#P4258. [WC2016] 挑战NPC

    ID: 3187 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2016WC/CTSC/集训队Special Judge图的建立,建图一般图的最大匹配

[WC2016] 挑战NPC

Description

Little N has recently been studying NP-complete problems. Seeing him so absorbed, Little O gave him such a problem:

There are nn balls, numbered from 11 to nn. There are also mm baskets, numbered from 11 to mm. Each basket can hold at most 33 balls.

Each ball can only be placed into specific baskets. Specifically, there are ee constraints. The ii-th constraint is described by two integers viv_i and uiu_i, meaning that ball viv_i can be placed into basket uiu_i.

Every ball must be placed into some basket. If a basket contains at most 11 ball, we call such a basket half-empty.

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

Little N was stunned and lost his train of thought. Little I, watching from the side, chuckled, “Easy problem!” Then, with a few words, he described a polynomial-time algorithm.

Little N was shocked. Three seconds later, he slapped the table: “No way! This problem is clearly NP-complete. Your algorithm must be wrong!”

Little I smiled slightly: “Then I’ll wait for my Turing Award!”

Little O can make problems but not solve them, so he turned to you—please investigate this problem and write a program to solve it.

Input Format

In the input file npc.in\tt{npc.in}, the first line contains a single positive integer TT, indicating there are TT test cases.

For each test case, the first line contains three positive integers nn, mm, ee, representing the number of balls, the number of baskets, and the number of constraints.

The next ee lines each contain two integers viv_i, uiu_i, indicating that ball viv_i can be placed into basket uiu_i.

Output Format

The output file is npc.out\tt{npc.out}.

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

Then output one more line containing nn integers p1,p2,...,pnp_1, p_2, ... , p_n, separated by spaces, describing one optimal arrangement. Here pip_i indicates that ball ii is placed into basket pip_i. If there are multiple optimal solutions, any one of them may be printed.

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

Hint

For all testdata, T5T \leq 5, 1n3m1 \leq n \leq 3 m. It is guaranteed that 1vin1 \leq v_i \leq n, 1uim1 \leq u_i \leq m, and there are no duplicate constraints.

It is guaranteed that there exists at least one feasible solution in which every ball is placed into a basket and the number of balls in each basket does not exceed 33.

Each test point satisfies the following conventions: ::cute-table{tuack} |Test point|m m |Notes | |:-:|:--------:|:--------:| |1 |10\leq 10 |n20,e25n \leq 20, e \leq 25| |2 |^ |^ | |3 |100\leq 100|e=nme = n m | |4 |^ |There exists a solution with mm half-empty baskets.| |5 |^ |No solution has any half-empty basket.| |6 |^ |^ | |7 |^ |None | |8 |^ |^ | |9 |^ |^ | |10 |^ |^ |

Translated by ChatGPT 5