#P7045. 「MCOI-03」金牌

    ID: 5892 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>交互题Special JudgeO2优化众数构造洛谷月赛

「MCOI-03」金牌

Description

Bookworm is organizing his nn gold medals, and he finds that all the medals are magnetic. Formally, each medal belongs to a type of magnetic pole, and there are many types of poles. If two adjacent medals have the same pole, they repel each other; otherwise, they attract each other.

Bookworm does not know the pole of each medal. He can only find out whether two medals have the same pole or different poles by bringing them close. In other words, you may perform at most QQ interactions. In each interaction, you ask the judge two numbers x,yx,y, and the judge returns whether the xx-th medal and the yy-th medal repel or attract. The medals are numbered from 00 to n1n-1.

Bookworm wants to arrange his medals into a permutation such that every pair of adjacent medals attracts each other. Please help him output any valid permutation, or report that there is no solution.

Interaction Format

This problem contains multiple test cases. The first line of input contains an integer TT, representing the number of test cases.

For each test case, the first line contains two integers n,Qn,Q, representing the number of medals and the upper limit of interactions.

If you need to make a query to the judge, output one line with two integers x,yx,y separated by spaces and flush the buffer. How to flush the buffer is described in the hint below. Then, read an integer retret from standard input. If ret=1ret=1, the xx-th medal and the yy-th medal attract; if ret=0ret=0, they repel.

If you have determined that there is no solution, output a line with a single integer 1-1 and flush the buffer. Then this test case ends, and you should proceed to the next test case.

If you have determined that a solution exists, first output a line with a single integer nn, then output any valid permutation of the medals on the next line, and flush the buffer. Then this test case ends, and you should proceed to the next test case.

After processing all TT test cases, you should terminate the program immediately. Extra output may cause RE.

Input Format

See "Interaction Format".

Output Format

See "Interaction Format".

2
3 100

1

1

1


2 100

0


0 1

0 2

1 2

3
0 1 2

0 1

-1

Hint

Explanation of Sample 1

There are two test cases in the sample. In the first test case, there are three medals. After three interactions, it is known that their poles are all different, so any permutation is correct.

In the second test case, the two medals have the same pole, so there is no solution.

Constraints

This problem uses bundled tests. The constraints are shown in the table below:

Test Point ID Q=Q= Special Property Score
11 n(n1)2\frac{n(n-1)}{2} n4n\ge 4 1010
22 2n22n-2 Each pole type has at most 22 medals 2020
33 The number of pole types is at most 33
44 3n3n None
55 2n22n-2 3030

For all test cases, 2n5×1042\le n\le5\times10^4, 1T5×1041\le T\le 5\times 10^4, Q105\sum Q\le 10^5.

Notes

You may use the following statements to flush the buffer:

  • For C/C++:fflush(stdout);
  • For C++:std::cout << std::flush;
  • For Java:System.out.flush();
  • For Python:stdout.flush();
  • For Pascal:flush(output);
  • For other languages, please refer to the corresponding language documentation.
  • In particular, for C++, using std::endl instead of '\n' when printing a newline can also flush the buffer automatically.

Translated by ChatGPT 5