IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)
G
Kevin and Teams
Codeforces
2900
原题
← F2H1 →
翻译中…

This is an interactive problem.

翻译中…

Kevin has $n$ classmates, numbered $1, 2, \ldots, n$. Any two of them may either be friends or not friends.

翻译中…

Kevin wants to select $2k$ classmates to form $k$ teams, where each team contains exactly $2$ people. Each person can belong to at most one team.

翻译中…

Let $u_i$ and $v_i$ be two people in the $i$-th team. To avoid potential conflicts during team formation, the team members must satisfy one of the following two conditions:

翻译中…
  • For all $i$ ($1\leq i \leq k$), classmate $u_i$ and $v_i$ are friends.
  • For all $i$ ($1\leq i \leq k$), classmate $u_i$ and $v_i$ are not friends.
翻译中…

Kevin wants to determine the maximum $k$ such that, regardless of the friendship relationships among the $n$ people, he can always find $2k$ people to form the teams. After that, he needs to form $k$ teams. But asking whether two classmates are friends is awkward, so Kevin wants to achieve this while asking about the friendship status of no more than $n$ pairs of classmates.

翻译中…

The interactor is adaptive. It means that the hidden relationship between classmates is not fixed before the interaction and will change during the interaction.

翻译中…

Interaction

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains one positive integer $n$ ($2\leq n \leq 10^5$) — the number of classmates.

翻译中…

First, you should output an integer $k$ ($1\leq k\leq \frac{n}2$) — the maximum number of teams you can form.

翻译中…

You can make queries in the following way — print one line of the form $?\;u\;v$ where $1\leq u\neq v \leq n$. After that, read a single integer: $0$ or $1$ indicating whether they are friends. $1$ means they are friends while $0$ means not.

翻译中…

If you want to print the answer, output $!\;u_1\;v_1\;u_2\;v_2\;\ldots\;u_k\;v_k$. You should output exactly $2k$ distinct numbers. Then, the interaction continues with the next test case.

翻译中…

You can make at most $n$ queries. Printing the answer does not count towards the number of queries made.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

翻译中…

After printing each query do not forget to output the end of line and flush$^{\text{∗}}$ the output. Otherwise, you will get Idleness limit exceeded verdict.

翻译中…

If, at any interaction step, you read $-1$ instead of valid data, your solution must exit immediately. This means that your solution will reveive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.

翻译中…

Hacks

翻译中…

The interactor for hacks is not adaptive. To make a hack, use the following format.

翻译中…

The first line contains the word "manual".

翻译中…

The second line contains a single integer $t$ ($1\leq t \leq 10^4$) — the number of test cases.

翻译中…

The first line of each test case contains an integer $n$ ($2\leq n \leq 10^5$) — the number of classmates.

翻译中…

The second line of each test case contains a string $s$ consisting of '0' and '1' of length $\frac{n(n - 1)}{2}$ — indicating the relationship between $(1, 2)$, $(1, 3)$, $\ldots$, $(1, n)$, $(2, 3)$, $(2, 4)$, $\ldots$, $(2, n)$, $\ldots$, $(n - 1, n)$. '1' means being friends and '0' means not being friends.

翻译中…

The following is the input of example.

翻译中…

manual
2
3
011
5
1011101011

翻译中…

The sum of $n$ over all test cases must not exceed $10^5$.

翻译中…

There is an additional constraint for hacks: The sum of $\frac{n(n-1)}{2}$ over all test cases must not exceed $10^7$.

翻译中…

$^{\text{∗}}$To flush, use: fflush(stdout) or cout.flush() in C++; sys.stdout.flush() in Python; see the documentation for other languages.

翻译中…

Note

翻译中…

In the first test case:

翻译中…

Kevin claims he can form $1$ team regardless of the friendship relationships among the $3$ people.

翻译中…

Kevin asks about the friendship relationship between people $1$ and $2$. The jury responds that they are friends.

翻译中…

Kevin answers that he can form a team with people $1$ and $2$.

翻译中…

In the second test case:

翻译中…

Kevin claims he can form $2$ teams regardless of the friendship relationships among the $5$ people.

翻译中…

Kevin asks about the friendship relationship between people $(1, 2), (3, 4), (3, 5), (1, 3), (2, 4)$. The jury responds with $1, 0, 1, 0, 0$.

翻译中…

Kevin answers that he can form two teams with people $(1, 2)$ and $(3, 5)$.

翻译中…

It is also possible to form two teams with people $(1, 3)$ and $(2, 4)$, since they are both not friends.

翻译中…

样例

Input #1
2
3

1

5

1

0

1

0

0
Output #1
1
? 1 2

! 1 2

2
? 1 2

? 3 4

? 3 5

? 1 3

? 2 4

! 1 2 3 5
InputCopy OutputCopy
题目信息
题号
2061G
时间限制
4 s
内存限制
256 MB
标签
构造
图论
交互题

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。