#P10748. [SEERC 2020] Mistake

[SEERC 2020] Mistake

Problem Description

You have kk machines. Each machine stores the nn numbers 1n1 \sim n in an unordered way. When you start a machine, it prints the frontmost number among the numbers stored in that machine, and then deletes it.

Fortunately, you know mm pairs (ai,bi)(a_i, b_i), meaning that in every machine’s storage, each stored aia_i is before bib_i.

You also know the output produced by starting machines one by one. For each printed number, determine a plan that specifies which machine it came from, and make sure that the number of occurrences of each number =k= k.

There may be multiple feasible solutions; output any one to get accepted.

Input Format

The first line contains three integers $n, k, m\ (1 \leq n, k \leq 5 \times 10^5, 0 \leq m \leq 2.5 \times 10^5, 1 \leq n \times k \leq 5 \times 10^5)$.

Then follow mm lines, each containing two integers ai,bi (1ai,bin)a_i, b_i\ (1 \leq a_i, b_i \leq n), and it is guaranteed that there is no cycle.

Then one line contains n×kn \times k numbers, representing the sequence printed by the machines when they are started in order.

Output Format

Output one valid starting plan.

3 3 2
1 2
1 3
1 1 2 3 3 2 1 2 3
1 2 2 1 2 1 3 3 3

Hint

Translated by ChatGPT 5