CF1919H.Tree Diameter
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a hidden tree with n vertices. The n−1 edges of the tree are numbered from 1 to n−1 . You can ask the following queries of two types:
- Give the grader an array a with n−1 positive integers. For each edge from 1 to n−1 , the weight of edge i is set to ai . Then, the grader will return the length of the diameter † .
- Give the grader two indices 1≤a,b≤n−1 . The grader will return the number of edges between edges a and b . In other words, if edge a connects ua and va while edge b connects ub and vb , the grader will return min(dist(ua,ub),dist(va,ub),dist(ua,vb),dist(va,vb)) , where dist(u,v) represents the number of edges on the path between vertices u and v .
Find any tree isomorphic ‡ to the hidden tree after at most n queries of type 1 and n queries of type 2 in any order.
† The distance between two vertices is the sum of the weights on the unique simple path that connects them. The diameter is the largest of all those distances.
‡ Two trees, consisting of n vertices each, are called isomorphic if there exists a permutation p containing integers from 1 to n such that edge ( u , v ) is present in the first tree if and only if the edge ( pu , pv ) is present in the second tree.
输入格式
The first and only line of input contains a single integer n ( 3≤n≤1000 ) — the number of vertices in the tree.
输出格式
Begin the interaction by reading n .
You are allowed to make queries in the following way:
- " ?1a1a2…an−1 " ( 1≤ai≤109 ). Then, you should read an integer k which represents the length of the diameter. You are only allowed to ask this query at most n times.
- " ?2ab " ( 1≤a,b≤n−1 ). Then, you should read an integer k which represents the number of edges between edges a and b . You are only allowed to ask this query at most n times.
In case your query is invalid. the program will terminate immediately and you will receive Wrong answer verdict.
To give the final answer, print "!" on a single line, followed by n−1 lines where line i contains " uivi " ( 1≤ui,vi≤n ) which represents that for each i from 1 to n−1 , there is an edge between ui and vi .
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.
Hacks
The first line contains a single integer n ( 3≤n≤1000 ) — the number of vertices in the tree.
The next n−1 lines contain two integers each ui,vi ( 1≤ui,vi≤n ) — the edges of the tree.
输入输出样例
输入#1
5 3 1 9 0
输出#1
? 1 1 1 1 1 ? 2 1 3 ? 1 4 3 2 1 ? 2 4 2 ! 3 1 4 2 1 2 2 5
说明/提示
The hidden tree in the example is shown above. The number on the vertices represents the vertex number while the number on the edges represents the edge number.
In the first query, all the edges are set to weight 1 , so the diameter has length 3 as shown in the diagram.
In the second query, there is 1 edge between edges 1 and 3 .
In the third query, the diameter is 9 by taking edges 1 , 2 and 3 .
In the fourth query, there are no edges between edges 4 and 2 .
The answer given in the example is shown in the above diagram. Since it is isomorphic to the hidden tree, it is accepted as a correct answer. Note that the edges can be printed in any order.