311383: CF1977E. Tensor

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Tensortime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

This is an interactive problem.

You are given an integer $n$.

The jury has hidden from you a directed graph with $n$ vertices (numbered from $1$ to $n$) and some number of edges. You additionally know that:

  • The graph only contains edges of the form $i \leftarrow j$, where $1 \le i < j \le n$.
  • For any three vertices $1 \le i < j < k \le n$, at least one of the following holds$^\dagger$:
    • Vertex $i$ is reachable from vertex $j$, or
    • Vertex $i$ is reachable from vertex $k$, or
    • Vertex $j$ is reachable from vertex $k$.

You want to color each vertex in either black or white such that for any two vertices $i$ and $j$ ($1 \le i < j \le n$) of the same color, vertex $i$ is reachable from vertex $j$.

To do that, you can ask queries of the following type:

  • ? i j — is vertex $i$ reachable from vertex $j$ ($1 \le i < j \le n$)?

Find any valid vertex coloring of the hidden graph in at most $2 \cdot n$ queries. It can be proven that such a coloring always exists.

Note that the grader is not adaptive: the graph is fixed before any queries are made.

$^\dagger$ Vertex $a$ is reachable from vertex $b$ if there exists a path from vertex $b$ to vertex $a$ in the graph.

Input

Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The description of the test cases follows.

The only line of each test case contains a single integer $n$ ($3 \le n \le 100$) — the number of vertices in the hidden graph.

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

Interaction

The interaction for each test case begins by reading the integer $n$.

To make a query, output "? i j" without quotes ($1 \le i < j \le n$). If vertex $i$ is reachable from vertex $j$, you will get YES as an answer. Otherwise, you will get NO as an answer.

If you receive the integer $-1$ instead of an answer or a valid value of $n$, it means your program has made an invalid query, has exceeded the limit of queries, or has given an incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

When you are ready to give the final answer, output "! $c_1 \ c_2 \ \ldots \ c_n$" without quotes — the colors of the vertices, where $c_i = 0$ if the vertex is black, and $c_i = 1$ if the vertex is white. After solving all test cases, your program should be terminated immediately.

After printing a query, do not forget to output an end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages.

Hacks

To hack, use the following format:

The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases.

The first line of each test case contains two integers $n$ and $m$ ($3 \le n \le 100$, $0 \le m \le \frac{n\cdot(n - 1)}{2}$) — the number of vertices and edges in the graph.

Each of the following $m$ lines should contain two integers $a$ and $b$ ($1 \le b < a \le n$), indicating that there is the edge $a \rightarrow b$ in the graph. The graph should satisfy the conditions above.

The sum of $n$ over all test cases should not exceed $1000$.

ExampleInput
2
4

YES

YES

YES

NO

NO

NO

5

Output
? 1 2

? 2 3

? 1 3

? 1 4

? 2 4

? 3 4

! 0 0 0 1

! 1 1 0 1 0
Note

The hidden graph in the first test case:

The hidden graph in the second test case:

The interaction happens as follows:

SolutionJuryExplanation
2There are $2$ test cases.
4In the first test case, the graph has $4$ vertices.
? 1 2 YESThe solution asks if vertex $1$ is reachable from vertex $2$, and the jury answers YES.
? 2 3 YESThe solution asks if vertex $2$ is reachable from vertex $3$, and the jury answers YES.
? 1 3 YESThe solution asks if vertex $1$ is reachable from vertex $3$, and the jury answers YES.
? 1 4 NOThe solution asks if vertex $1$ is reachable from vertex $4$, and the jury answers NO.
? 2 4 NOThe solution asks if vertex $2$ is reachable from vertex $4$, and the jury answers NO.
? 3 4 NOThe solution asks if vertex $3$ is reachable from vertex $4$, and the jury answers NO.
! 0 0 0 1The solution has somehow determined a valid coloring and outputs it. Since the output is correct, the jury continues to the next test case.
5In the second test case, the graph has $5$ vertices.
! 1 1 0 1 0The solution has somehow determined a valid coloring, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit.

Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.

Output

题目大意:

这是一个交互式问题。你被给定一个整数 n。陪审团隐藏了一个有 n 个顶点(编号从 1 到 n)和若干条边的有向图。你另外知道:

- 图只包含形式为 i <- j 的边,其中 1 ≤ i < j ≤ n。
- 对于任意三个顶点 1 ≤ i < j < k ≤ n,至少满足以下条件之一:
- 顶点 i 从顶点 j 可达,或
- 顶点 i 从顶点 k 可达,或
- 顶点 j 从顶点 k 可达。

你想要将每个顶点染成黑色或白色,使得对于任意两个相同颜色的顶点 i 和 j(1 ≤ i < j ≤ n),顶点 i 从顶点 j 可达。

为此,你可以询问以下类型的查询:

- ? i j — 顶点 i 从顶点 j 可达吗(1 ≤ i < j ≤ n)?

在不超过 2 * n 次查询内找到隐藏图的任何有效顶点着色。可以证明这样的着色总是存在的。

注意,评分器不是自适应的:在提出任何查询之前,图是固定的。

输入输出数据格式:

输入:

每个测试包含多个测试案例。输入的第一行包含一个整数 t(1 ≤ t ≤ 1000)— 测试案例的数量。测试案例的描述随之而来。

每个测试案例的唯一一行包含一个整数 n(3 ≤ n ≤ 100)— 隐藏图的顶点数。

保证所有测试案例的 n 之和不超过 1000。

交互:

每个测试案例开始时读取整数 n。

为了进行查询,输出 "? i j"(1 ≤ i < j ≤ n)。如果顶点 i 从顶点 j 可达,你将得到 YES 作为答案。否则,你将得到 NO 作为答案。

如果你收到整数 -1 而不是答案或有效的 n 值,这意味着你的程序提出了无效查询,超过了查询限制,或在上一个测试案例中给出了错误答案。你的程序必须立即终止以获得错误答案的裁决。否则,你可能会得到任意裁决,因为你的解决方案将继续从已关闭的流中读取。

当你准备给出最终答案时,输出 "! c_1 c_2 ... c_n" — 顶点的颜色,其中 c_i = 0 如果顶点是黑色,c_i = 1 如果顶点是白色。解决所有测试案例后,你的程序应立即终止。

在打印查询后,不要忘记输出换行符并刷新输出。否则,你将得到 Idleness limit exceeded。为此,使用:

- 在 C++ 中:fflush(stdout)cout.flush()
- 在 Java 中:System.out.flush()
- 在 Pascal 中:flush(output)
- 在 Python 中:stdout.flush()
- 查看其他语言的文档。

破解:

使用以下格式:

破解的第一行包含一个整数 t(1 ≤ t ≤ 1000)— 测试案例的数量。

每个测试案例的第一行包含两个整数 n 和 m(3 ≤ n ≤ 100,0 ≤ m ≤ n*(n - 1)/2)— 图的顶点和边的数量。

接下来的 m 行应每行包含两个整数 a 和 b(1 ≤ b < a ≤ n),表示图中存在边 a -> b。图应满足上述条件。

所有测试案例的 n 之和不应超过 1000。

示例:

输入:

```
2
4
YES
YES
YES
NO
NO
NO
5
```

输出:

```
? 1 2
? 2 3
? 1 3
? 1 4
? 2 4
? 3 4
! 0 0 0 1
! 1 1 0 1 0
```题目大意: 这是一个交互式问题。你被给定一个整数 n。陪审团隐藏了一个有 n 个顶点(编号从 1 到 n)和若干条边的有向图。你另外知道: - 图只包含形式为 i <- j 的边,其中 1 ≤ i < j ≤ n。 - 对于任意三个顶点 1 ≤ i < j < k ≤ n,至少满足以下条件之一: - 顶点 i 从顶点 j 可达,或 - 顶点 i 从顶点 k 可达,或 - 顶点 j 从顶点 k 可达。 你想要将每个顶点染成黑色或白色,使得对于任意两个相同颜色的顶点 i 和 j(1 ≤ i < j ≤ n),顶点 i 从顶点 j 可达。 为此,你可以询问以下类型的查询: - ? i j — 顶点 i 从顶点 j 可达吗(1 ≤ i < j ≤ n)? 在不超过 2 * n 次查询内找到隐藏图的任何有效顶点着色。可以证明这样的着色总是存在的。 注意,评分器不是自适应的:在提出任何查询之前,图是固定的。 输入输出数据格式: 输入: 每个测试包含多个测试案例。输入的第一行包含一个整数 t(1 ≤ t ≤ 1000)— 测试案例的数量。测试案例的描述随之而来。 每个测试案例的唯一一行包含一个整数 n(3 ≤ n ≤ 100)— 隐藏图的顶点数。 保证所有测试案例的 n 之和不超过 1000。 交互: 每个测试案例开始时读取整数 n。 为了进行查询,输出 "? i j"(1 ≤ i < j ≤ n)。如果顶点 i 从顶点 j 可达,你将得到 YES 作为答案。否则,你将得到 NO 作为答案。 如果你收到整数 -1 而不是答案或有效的 n 值,这意味着你的程序提出了无效查询,超过了查询限制,或在上一个测试案例中给出了错误答案。你的程序必须立即终止以获得错误答案的裁决。否则,你可能会得到任意裁决,因为你的解决方案将继续从已关闭的流中读取。 当你准备给出最终答案时,输出 "! c_1 c_2 ... c_n" — 顶点的颜色,其中 c_i = 0 如果顶点是黑色,c_i = 1 如果顶点是白色。解决所有测试案例后,你的程序应立即终止。 在打印查询后,不要忘记输出换行符并刷新输出。否则,你将得到 Idleness limit exceeded。为此,使用: - 在 C++ 中:fflush(stdout)cout.flush(); - 在 Java 中:System.out.flush(); - 在 Pascal 中:flush(output); - 在 Python 中:stdout.flush(); - 查看其他语言的文档。 破解: 使用以下格式: 破解的第一行包含一个整数 t(1 ≤ t ≤ 1000)— 测试案例的数量。 每个测试案例的第一行包含两个整数 n 和 m(3 ≤ n ≤ 100,0 ≤ m ≤ n*(n - 1)/2)— 图的顶点和边的数量。 接下来的 m 行应每行包含两个整数 a 和 b(1 ≤ b < a ≤ n),表示图中存在边 a -> b。图应满足上述条件。 所有测试案例的 n 之和不应超过 1000。 示例: 输入: ``` 2 4 YES YES YES NO NO NO 5 ``` 输出: ``` ? 1 2 ? 2 3 ? 1 3 ? 1 4 ? 2 4 ? 3 4 ! 0 0 0 1 ! 1 1 0 1 0 ```

加入题单

上一题 下一题 算法标签: