CF1738F.Connectivity Addicts

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is an interactive problem.

Given a simple undirected graph with nn vertices numbered from 11 to nn, your task is to color all the vertices such that for every color cc, the following conditions hold:

  1. The set of vertices with color cc is connected;
  2. sc≤nc2s_c \leq n_c^2, where ncn_c is the number of vertices with color cc, and scs_c is the sum of degrees of vertices with color cc.

It can be shown that there always exists a way to color all the vertices such that the above conditions hold.

Initially, you are only given the number nn of vertices and the degree of each vertex.

In each query, you can choose a vertex uu. As a response, you will be given the kk-th edge incident to uu, if this is the kk-th query on vertex uu.

You are allowed to make at most nn queries.

An undirected graph is simple if it does not contain multiple edges or self-loops.

The degree of a vertex is the number of edges incident to it.

A set SS of vertices is connected if for every two different vertices u,v∈Su, v \in S, there is a path, which only passes through vertices in SS, that connects uu and vv. That is, there is a sequence of edges (u1,v1),(u2,v2),…,(uk,vk)(u_1, v_1), (u_2, v_2), \dots, (u_k, v_k) with k≥1k \geq 1 such that

  1. u1=uu_1 = u, vk=vv_k = v, and vi=ui+1v_i = u_{i+1} for every 1≤i<k1 \leq i \lt k; and
  2. uk∈Su_k \in S and vk∈Sv_k \in S for every 1≤i≤k1 \leq i \leq k.

Especially, a set containing only one vertex is connected.

Interaction

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases. The following lines contain the description and the interactive section of each test case.

For each test case, you begin the interaction by reading an integer nn (1≤n≤10001\le n \le 1000) in the first line, indicating the number of vertices in the graph.

The second line contains nn integers d1,d2,…,dnd_1, d_2, \dots, d_n (0≤di≤n−10 \leq d_i \leq n - 1), where did_i is the degree of vertex ii.

To make a query on vertex uu (1≤u≤n1 \leq u \leq n), you should output

  • "? uu"

in a separate line. If this is the kk-th query on vertex uu, vertex eu,ke_{u, k} will be given in the next separate line, where (u,eu,k)\left(u, e_{u, k}\right) is the kk-th edge incident to vertex uu. In case of k>duk \gt d_u, define eu,k=−1e_{u, k} = -1. You should make no more than nn "?" queries.

To give the answer, you should output

  • "! c1c_1 c2c_2 …\dots cnc_n"

in a separate line, where cic_i (1≤ci≤n1 \leq c_i \leq n) is the color of vertex ii. After that, your program should continue to the next test case, or terminate if this is the last test case.

It is guaranteed that the graph is a simple undirected graph.

It is guaranteed that the sum of nn over all test cases does not exceed 10001000.

In case your query format is invalid, or you have made more than nn "?" queries, you will receive Wrong Answer verdict.

After printing a query, do not forget to output 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.

Hack Format

The first line of the hack contains an integer tt (1≤t≤10001 \leq t \leq 1000), indicating the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains an integer nn (1≤n≤10001 \leq n \leq 1000), indicating the number of vertices in the graph.

Then nn lines follow. The ii-th line contains an integer did_i (0≤di≤n−10 \leq d_i \leq n - 1), indicating the degree of vertex ii, and then did_i distinct integers ei,1,ei,2,…,ei,die_{i,1}, e_{i,2}, \dots, e_{i,d_i} (1≤ei,j≤n1 \leq e_{i, j} \leq n and ei,j≠ie_{i,j} \neq i), where (i,ei,j)\left(i, e_{i,j}\right) is the jj-th edge incident to vertex ii.

It should be guaranteed that the graph is a simple undirected graph.

It should be guaranteed that the sum of nn over all test cases does not exceed 10001000.

这是一个交互式问题。

给定一个包含 nn 个顶点(编号为 11 到 nn)的简单无向图,你的任务是为所有顶点染色,使得对每种颜色 cc,以下条件均成立:

  1. 颜色为 cc 的顶点集合是连通的;
  2. sc≤nc2s_c \leq n_c^2,其中 ncn_c 表示颜色为 cc 的顶点数,scs_c 表示颜色为 cc 的所有顶点的度数之和。

可以证明,总存在一种染色方案满足上述条件。

初始时,你仅被给定顶点数 nn 以及每个顶点的度数。

每次查询中,你可以选择一个顶点 uu。作为响应,若这是你在顶点 uu 上进行的第 kk 次查询,则你会得到与 uu 相连的第 kk 条边。

你最多可进行 nn 次查询。

一个无向图被称为“简单图”,当且仅当它不含重边或自环。

一个顶点的度数是指与其关联的边的数量。

顶点集合 SS 是连通的,当且仅当对任意两个不同的顶点 u,v∈Su, v \in S,均存在一条仅经过 SS 中顶点的路径连接 uu 和 vv。即存在一系列边 (u1,v1),(u2,v2),…,(uk,vk)(u_1, v_1), (u_2, v_2), \dots, (u_k, v_k)(其中 k≥1k \geq 1),满足:

  1. u1=uu_1 = u,vk=vv_k = v,且对每个 1≤i<k1 \leq i < k,有 vi=ui+1v_i = u_{i+1};
  2. 对每个 1≤i≤k1 \leq i \leq k,均有 ui∈Su_i \in S 且 vi∈Sv_i \in S。

特别地,仅含一个顶点的集合是连通的。

交互方式

每个测试包含多个测试用例。第一行是一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例数量。随后若干行描述并交互每个测试用例。

对每个测试用例,你首先读入第一行的一个整数 nn(1≤n≤10001\le n \le 1000),表示图中顶点数量。

第二行包含 nn 个整数 d1,d2,…,dnd_1, d_2, \dots, d_n(0≤di≤n−10 \leq d_i \leq n - 1),其中 did_i 是顶点 ii 的度数。

要对顶点 uu(1≤u≤n1 \leq u \leq n)进行一次查询,你应在单独一行输出:

  • "? uu"

若这是你在顶点 uu 上进行的第 kk 次查询,则下一行将给出顶点 eu,ke_{u, k},其中 (u,eu,k)\left(u, e_{u, k}\right) 是与顶点 uu 相连的第 kk 条边。若 k>duk > d_u,则定义 eu,k=−1e_{u, k} = -1。你至多进行 nn 次以 "?" 开头的查询。

要提交答案,你应在单独一行输出:

  • "! c1c_1 c2c_2 …\dots cnc_n"

其中 cic_i(1≤ci≤n1 \leq c_i \leq n)表示顶点 ii 的颜色。之后,你的程序应继续处理下一个测试用例;若当前已是最后一个测试用例,则应终止运行。

保证该图是一个简单无向图。

保证所有测试用例的 nn 值之和不超过 10001000。

若你的查询格式非法,或 "?" 查询次数超过 nn 次,你将收到“Wrong Answer”判据。

每次输出查询后,请务必输出换行符并刷新输出缓冲区。否则,你将收到“Idleness limit exceeded”。为此,请使用:

  • C++ 中的 fflush(stdout) 或 cout.flush();
  • Java 中的 System.out.flush();
  • Pascal 中的 flush(output);
  • Python 中的 stdout.flush();
  • 其他语言请参考相应文档。

Hack 输入格式

Hack 输入的第一行是一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例数量。随后若干行描述每个测试用例。

每个测试用例的第一行是一个整数 nn(1≤n≤10001 \leq n \leq 1000),表示图中顶点数量。

接下来 nn 行,第 ii 行首先是一个整数 did_i(0≤di≤n−10 \leq d_i \leq n - 1),表示顶点 ii 的度数;随后是 did_i 个互不相同的整数 ei,1,ei,2,…,ei,die_{i,1}, e_{i,2}, \dots, e_{i,d_i}(1≤ei,j≤n1 \leq e_{i, j} \leq n 且 ei,j≠ie_{i,j} \neq i),其中 (i,ei,j)\left(i, e_{i,j}\right) 是与顶点 ii 相连的第 jj 条边。

需保证该图是一个简单无向图。

需保证所有测试用例的 nn 值之和不超过 10001000。

输入输出样例

  • 输入#1

    1
    5
    2 2 2 2 0
    
    2
    
    4
    
    2
    
    4

    输出#1

    ? 1
    
    ? 1
    
    ? 3
    
    ? 3
    
    ! 1 1 2 2 3

说明/提示

In the example, there is only one test case.

In the test case, there are n=5n = 5 vertices with vertices 1,2,3,41, 2, 3, 4 of degree 22 and vertex 55 of degree 00. It is obvious that vertex 55 is isolated, i.e., it does not connect to any other vertices.

A possible interaction is shown in the sample input and output, where 44 "?" queries are made on vertex 11 twice and vertex 33 twice. According to the responses to these queries, we know that each of vertex 11 and vertex 33 connects to two vertices 22 and 44.

A possible solution is shown in the sample output, where vertex 11 and vertex 22 are colored by 11, vertex 33 and vertex 44 are colored by 22, and vertex 55 is colored by 33. It can be seen that this solution satisfies the required conditions as follows.

  • For color c=1c = 1, vertex 11 and vertex 22 are connected. Moreover, n1=2n_1 = 2 and s1=d1+d2=2+2=4≤n12=22=4s_1 = d_1 + d_2 = 2 + 2 = 4 \leq n_1^2 = 2^2 = 4;
  • For color c=2c = 2, vertex 33 and vertex 44 are connected. Moreover, n2=2n_2 = 2 and s2=d3+d4=2+2=4≤n22=22=4s_2 = d_3 + d_4 = 2 + 2 = 4 \leq n_2^2 = 2^2 = 4;
  • For color c=3c = 3, there is only one vertex (vertex 55) colored by 33. Moreover, n3=1n_3 = 1 and s3=d5=0≤n32=12=1s_3 = d_5 = 0 \leq n_3^2 = 1^2 = 1.

在该示例中,仅有一个测试用例。

在该测试用例中,共有 n=5n = 5 个顶点,其中顶点 1,2,3,41, 2, 3, 4 的度数均为 22,顶点 55 的度数为 00。显然,顶点 55 是孤立的,即它不与任何其他顶点相连。

样例输入与输出中展示了一种可能的交互过程:共进行了 44 次 "?" 查询,其中对顶点 11 查询两次、对顶点 33 查询两次。根据这些查询的响应,我们得知顶点 11 和顶点 33 均分别与顶点 22 和顶点 44 相连。

样例输出中给出了一种可能的解法:顶点 11 和顶点 22 被染为颜色 11,顶点 33 和顶点 44 被染为颜色 22,顶点 55 被染为颜色 33。可以验证该解法满足题目所要求的条件,具体如下:

  • 对于颜色 c=1c = 1,顶点 11 和顶点 22 相连;此外,n1=2n_1 = 2,且 s1=d1+d2=2+2=4≤n12=22=4s_1 = d_1 + d_2 = 2 + 2 = 4 \leq n_1^2 = 2^2 = 4;
  • 对于颜色 c=2c = 2,顶点 33 和顶点 44 相连;此外,n2=2n_2 = 2,且 s2=d3+d4=2+2=4≤n22=22=4s_2 = d_3 + d_4 = 2 + 2 = 4 \leq n_2^2 = 2^2 = 4;
  • 对于颜色 c=3c = 3,仅有唯一一个顶点(即顶点 55)被染为颜色 33;此外,n3=1n_3 = 1,且 s3=d5=0≤n32=12=1s_3 = d_5 = 0 \leq n_3^2 = 1^2 = 1。

输入解题思路,AI测评打分。不知道怎么写?

首页