Spectral::Cup 2026 Round 3 (Codeforces Round 1110, Div. 1 + Div. 2)
G
NPC 挑战
NPC Challenge
Codeforces
3000
原题
← FH →

NPC 挑战AI (CI翻译)

This is an interactive problem.

翻译中…

There is a hidden undirected tree consisting of $n$ vertices. To find this tree, you may ask queries of the following form:

翻译中…
  • Pick a sequence of distinct vertices $a_1, a_2, \ldots, a_k$, where $k \ge 1$.
翻译中…

The interactor will process your sequence and return a subset of these vertices, denoted by $S$. The set $S$ is generated by the following process:

翻译中…
  • Initially, $S$ is an empty set.
  • The interactor processes the vertices in the exact order they appear in your sequence, from $a_1$ to $a_k$.
  • For each $a_i$, if $a_i$ does not share an edge with any vertex that is currently in $S$, then $a_i$ is added to $S$. Otherwise, $a_i$ is ignored.
  • After processing all $k$ vertices, the interactor returns the final set $S$ to you. $S$ is represented by a binary string $s$ of length $k$, where $s_i=\texttt{1}$ if and only if $a_i \in S$.
翻译中…

Your task is to find all $n-1$ edges of the hidden tree. To make the problem harder, the sum of $k$ over all queries must not exceed $30 \cdot n$.

翻译中…

Input

输入格式

AI (CI翻译)

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

每个测试包含多个测试用例。第一行包含测试用例数量 $t$($1 \le t \le 100$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains an integer $n$ ($2 \le n \le 10^3$), representing the number of vertices in the hidden tree.

每个测试用例的第一行包含一个整数 $n$($2 \le n \le 10^3$),表示隐藏树中的顶点数。AI (CI翻译)

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

保证所有测试用例的 $n$ 之和不超过 $10^3$。AI (CI翻译)

Interaction

交互

AI (CI翻译)

To make a query, output a line in the following format:

要进行一次询问,请按照以下格式输出一行:AI (CI翻译)

  • $\mathtt{?}\;k\;a_1\;a_2\;\ldots\;a_k$ ($1 \le k \le n$, $1 \le a_i \le n$, $a_i \ne a_j$ for all $1 \le i \lt j \le k$)
  • $\mathtt{?}\;k\;a_1\;a_2\;\ldots\;a_k$($1 \le k \le n$,$1 \le a_i \le n$,对于所有 $1 \le i \lt j \le k$,均有 $a_i \ne a_j$)
AI (CI翻译)

As a response to the query, you will receive a binary string of length $k$, representing $S$.

作为询问的响应,你将收到一个长度为 $k$ 的二进制字符串,表示集合 $S$。AI (CI翻译)

To report the answer, first output $\mathtt{!}$ on a single line. Then, output $n-1$ lines, where the $i$-th line contains two integers $u_i$ and $v_i$, representing an undirected edge between vertices $u_i$ and $v_i$. You may output the edges in any order.

要报告答案,首先单独输出一行 $\mathtt{!}$。然后输出 $n-1$ 行,第 $i$ 行包含两个整数 $u_i$ 和 $v_i$,表示顶点 $u_i$ 与顶点 $v_i$ 之间的一条无向边。边可以按任意顺序输出。AI (CI翻译)

After this, proceed to the next test case or terminate if this is the last test case.

之后继续处理下一个测试用例;如果这是最后一个测试用例,则结束程序。AI (CI翻译)

The interactor is not adaptive. This means that the hidden tree is fixed before any queries are made and will not change throughout the interaction.

交互器是非自适应的。这意味着隐藏树在进行任何询问之前就已经固定,并且在整个交互过程中不会发生改变。AI (CI翻译)

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 receive 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.

输出每次询问后,请不要忘记输出换行并刷新$^{\text{∗}}$输出。否则,你将收到 Idleness limit exceeded 判定。如果在交互的任何步骤中,你读取到 $-1$ 而不是有效数据,你的程序必须立即退出。这意味着你的程序会因为无效询问或其他错误而收到 Wrong answer。如果不退出,程序将继续从已关闭的输入流中读取,可能会收到任意判定结果。AI (CI翻译)

Hacks

非交互式输入(Hacks)AI (CI翻译)

To hack, use the following format.

进行 hack 时,请使用以下格式。AI (CI翻译)

The first line shoud contain a single integer $t$ ($1 \le t \le 100$), representing the number of test cases.

第一行应包含一个整数 $t$($1 \le t \le 100$),表示测试用例数量。AI (CI翻译)

The first line of each test case should contain an integer $n$ ($2 \le n \le 10^3$), representing the number of vertices in the tree.

每个测试用例的第一行应包含一个整数 $n$($2 \le n \le 10^3$),表示树中的顶点数。AI (CI翻译)

Each of the next $n-1$ lines should contain two integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), representing an edge in the tree. You need to guarantee that the edges form a valid tree.

接下来的 $n-1$ 行中,每行应包含两个整数 $u$ 和 $v$($1 \le u,v \le n$,$u \ne v$),表示树中的一条边。你需要保证这些边构成一棵合法的树。AI (CI翻译)

The sum of $n$ over all test cases should not exceed $10^3$.

所有测试用例的 $n$ 之和不应超过 $10^3$。AI (CI翻译)

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

$^{\text{∗}}$刷新输出的方法:在 C++ 中使用 fflush(stdout) 或 cout.flush();在 Python 中使用 sys.stdout.flush();其他语言请参阅相关文档。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, the hidden tree consists of $n=2$ vertices, and the only edge is $(1,2)$.

在第一个测试用例中,隐藏树由 $n=2$ 个顶点组成,唯一的边为 $(1,2)$。AI (CI翻译)

  • For the first query, $a=[1,2]$:
  • Vertex $1$ is processed. Currently, $S$ is empty. Since $1$ has no neighbors in $S$, it is added to $S$. $S$ becomes $\{1\}$.
    • Vertex $2$ is processed. Its neighbor, vertex $1$, is already in $S$. Thus, vertex $2$ is ignored.
  • 对于第一次询问,$a=[1,2]$:
  • 处理顶点 $1$。此时 $S$ 为空。由于 $1$ 在 $S$ 中没有邻居,因此将其加入 $S$。此时 $S$ 变为 $\{1\}$。
    • 处理顶点 $2$。其邻居顶点 $1$ 已经在 $S$ 中。因此忽略顶点 $2$。
AI (CI翻译)

In the second test case, the hidden tree consists of $n=5$ vertices. The edges are $(1, 2)$, $(2, 3)$, $(2, 4)$, and $(3, 5)$.

在第二个测试用例中,隐藏树由 $n=5$ 个顶点组成。边为 $(1, 2)$、$(2, 3)$、$(2, 4)$ 和 $(3, 5)$。AI (CI翻译)

  • For the first query, $a=[1,2,5]$:
  • Vertex $1$ is processed. $S$ is empty. It is added to $S$. $S$ becomes $\{1\}$.
    • Vertex $2$ is processed. It shares an edge with vertex $1 \in S$. It is ignored.
    • Vertex $5$ is processed. Its only neighbor is vertex $3 \notin S$. It is added to $S$. $S$ becomes $\{1, 5\}$.
  • For the second query, $a=[5,3,4,2,1]$:
  • Vertex $5$ is processed. $S$ is empty. It is added to $S$. $S$ becomes $\{5\}$.
    • Vertex $3$ is processed. It shares an edge with vertex $5 \in S$. It is ignored.
    • Vertex $4$ is processed. Its only neighbor is vertex $2 \notin S$. It is added to $S$. $S$ becomes $\{4, 5\}$.
    • Vertex $2$ is processed. It shares an edge with vertex $4 \in S$. It is ignored.
    • Vertex $1$ is processed. Its only neighbor is vertex $2 \notin S$. Since it has no neighbors in $S$, it is added to $S$.
  • 对于第一次询问,$a=[1,2,5]$:
  • 处理顶点 $1$。此时 $S$ 为空。将其加入 $S$。此时 $S$ 变为 $\{1\}$。
    • 处理顶点 $2$。它与顶点 $1 \in S$ 相邻,因此忽略顶点 $2$。
    • 处理顶点 $5$。它唯一的邻居是顶点 $3 \notin S$。将其加入 $S$。此时 $S$ 变为 $\{1, 5\}$。
  • 对于第二次询问,$a=[5,3,4,2,1]$:
  • 处理顶点 $5$。此时 $S$ 为空。将其加入 $S$。此时 $S$ 变为 $\{5\}$。
    • 处理顶点 $3$。它与顶点 $5 \in S$ 相邻,因此忽略顶点 $3$。
    • 处理顶点 $4$。它唯一的邻居是顶点 $2 \notin S$。将其加入 $S$。此时 $S$ 变为 $\{4, 5\}$。
    • 处理顶点 $2$。它与顶点 $4 \in S$ 相邻,因此忽略顶点 $2$。
    • 处理顶点 $1$。它唯一的邻居是顶点 $2 \notin S$。由于它在 $S$ 中没有邻居,因此将其加入 $S$。
AI (CI翻译)

样例

Input #1
2
2

10

5

101

10101
Output #1
? 2 1 2

!
2 1

? 3 1 2 5

? 5 5 3 4 2 1

!
3 5
1 2
3 2
2 4
InputCopy OutputCopy
题目信息
题号
2245G
时间限制
4 s
内存限制
256 MB
标签
分治
交互题

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