CF1738F.Connectivity Addicts

普及/提高-

通过率:0%

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. scnc2s_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,vSu, 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 k1k \geq 1 such that

  1. u1=uu_1 = u , vk=vv_k = v , and vi=ui+1v_i = u_{i+1} for every 1i<k1 \leq i < k ; and
  2. ukSu_k \in S and vkSv_k \in S for every 1ik1 \leq i \leq k .

Especially, a set containing only one vertex is connected.

输入格式

输出格式

Each test contains multiple test cases. The first line contains an integer tt ( 1t10001 \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 ( 1n10001\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 ( 0din10 \leq d_i \leq n - 1 ), where did_i is the degree of vertex ii .

To make a query on vertex uu ( 1un1 \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 > 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 ( 1cin1 \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 ( 1t10001 \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 ( 1n10001 \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 ( 0din10 \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} ( 1ei,jn1 \leq e_{i, j} \leq n and ei,jie_{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 .

输入输出样例

  • 输入#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=4n12=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=4n22=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=0n32=12=1s_3 = d_5 = 0 \leq n_3^2 = 1^2 = 1 .
首页