#P10247. Pairing Pairs

    ID: 11428 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>图论洛谷原创Special Judge洛谷月赛

Pairing Pairs

Background

This problem uses the original contest testdata. If you want to test an O(n)O(n) solution, you can refer to the enhanced version.

Problem Description

You are given mm pairs (ui,vi)(u_i, v_i) (guaranteed that 1ui<vin1 \le u_i < v_i \le n, and all mm pairs are pairwise distinct). For each ii, find an index jj such that the four numbers ui,vi,uj,vju_i, v_i, u_j, v_j are all distinct from each other, or report that such a jj does not exist.

Input Format

The first line contains two positive integers n,mn, m, indicating the range of numbers in the pairs and the number of pairs.

The next mm lines each contain two positive integers ui,viu_i, v_i, representing the ii-th pair.

Output Format

Output one line with mm non-negative integers. The ii-th integer is the jj you found for this ii. If no such jj exists, output 00.

If there are multiple valid jj, output any one of them.

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

5 0 4 5 1

Hint

[Sample Explanation]

The answer is not unique. For example, when i=4i = 4:

  • jj can also be 33, because the four numbers 2,5,1,32, 5, 1, 3 are all distinct. So outputting 5 0 4 3 1 can also pass.
  • However, jj cannot be 11, because among 2,5,1,22, 5, 1, 2 there are repeated numbers.

[Constraints]

This problem uses bundled judging. Specifically, you can get the score of a subtask only if you pass all test points in that subtask.

Subtask ID nn \le mm \le Special property Score
11 10310^3 3×1033\times 10^3 1919
22 =103=10^3 =3×105=3\times 10^5 1616
33 3×1053\times 10^5 =n1=n-1 ui=i,vi=i+1u_i=i,v_i=i+1 33
44 vi=i+1v_i=i+1 2222
55 3×1053\times 10^5 Random testdata 1111
66 2929

The specific generation process for subtask 55: first I specify a set of n,mn, m, then repeat the following process mm times:

  • Draw xx from 1n1 \sim n, then draw yy from 1n11 \sim n-1.
  • If yxy \ge x, then increase yy by 11; otherwise swap x,yx, y.
  • Check whether the pair (x,y)(x, y) has appeared before. If yes, go back to the first step.
  • Output (x,y)(x, y).

For all data, it is guaranteed that 1ui<vin3×1051 \le u_i < v_i \le n \le 3\times 10^5, and 1m3×1051 \le m \le 3\times 10^5.

Translated by ChatGPT 5