CF1738F.Connectivity Addicts
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is an interactive problem.
Given a simple undirected graph with n vertices numbered from 1 to n , your task is to color all the vertices such that for every color c , the following conditions hold:
- The set of vertices with color c is connected;
- sc≤nc2 , where nc is the number of vertices with color c , and sc is the sum of degrees of vertices with color c .
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 n of vertices and the degree of each vertex.
In each query, you can choose a vertex u . As a response, you will be given the k -th edge incident to u , if this is the k -th query on vertex u .
You are allowed to make at most n 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 S of vertices is connected if for every two different vertices u,v∈S , there is a path, which only passes through vertices in S , that connects u and v . That is, there is a sequence of edges (u1,v1),(u2,v2),…,(uk,vk) with k≥1 such that
- u1=u , vk=v , and vi=ui+1 for every 1≤i<k ; and
- uk∈S and vk∈S for every 1≤i≤k .
Especially, a set containing only one vertex is connected.
输入格式
无
输出格式
Each test contains multiple test cases. The first line contains an integer t ( 1≤t≤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 n ( 1≤n≤1000 ) in the first line, indicating the number of vertices in the graph.
The second line contains n integers d1,d2,…,dn ( 0≤di≤n−1 ), where di is the degree of vertex i .
To make a query on vertex u ( 1≤u≤n ), you should output
- "? u "
in a separate line. If this is the k -th query on vertex u , vertex eu,k will be given in the next separate line, where (u,eu,k) is the k -th edge incident to vertex u . In case of k>du , define eu,k=−1 . You should make no more than n "?" queries.To give the answer, you should output
- "! c1 c2 … cn "
in a separate line, where ci ( 1≤ci≤n ) is the color of vertex i . 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 n over all test cases does not exceed 1000 .
In case your query format is invalid, or you have made more than n "?" 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 t ( 1≤t≤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 n ( 1≤n≤1000 ), indicating the number of vertices in the graph.
Then n lines follow. The i -th line contains an integer di ( 0≤di≤n−1 ), indicating the degree of vertex i , and then di distinct integers ei,1,ei,2,…,ei,di ( 1≤ei,j≤n and ei,j=i ), where (i,ei,j) is the j -th edge incident to vertex i .
It should be guaranteed that the graph is a simple undirected graph.
It should be guaranteed that the sum of n over all test cases does not exceed 1000 .
输入输出样例
输入#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=5 vertices with vertices 1,2,3,4 of degree 2 and vertex 5 of degree 0 . It is obvious that vertex 5 is isolated, i.e., it does not connect to any other vertices.
A possible interaction is shown in the sample input and output, where 4 "?" queries are made on vertex 1 twice and vertex 3 twice. According to the responses to these queries, we know that each of vertex 1 and vertex 3 connects to two vertices 2 and 4 .
A possible solution is shown in the sample output, where vertex 1 and vertex 2 are colored by 1 , vertex 3 and vertex 4 are colored by 2 , and vertex 5 is colored by 3 . It can be seen that this solution satisfies the required conditions as follows.
- For color c=1 , vertex 1 and vertex 2 are connected. Moreover, n1=2 and s1=d1+d2=2+2=4≤n12=22=4 ;
- For color c=2 , vertex 3 and vertex 4 are connected. Moreover, n2=2 and s2=d3+d4=2+2=4≤n22=22=4 ;
- For color c=3 , there is only one vertex (vertex 5 ) colored by 3 . Moreover, n3=1 and s3=d5=0≤n32=12=1 .