CF1930H.Interactive Mex Tree
NOI/NOI+/CTSC
通过率:0%
时间限制:5.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is an interactive problem.
Alice has a tree T consisting of n nodes, numbered from 1 to n. Alice will show T to Bob. After observing T, Bob needs to tell Alice two permutations p1 and p2 of [1,2,…,n].
Then, Alice will play q rounds of the following game.
- Alice will create an array a that is a permutation of [0,1,…,n−1]. The value of node v will be av.
- Alice will choose two nodes u and v (1≤u,v≤n, u=v) of T and tell them to Bob. Bob will need to find the MEX† of the values on the unique simple path between nodes u and v.
- To find this value, Bob can ask Alice at most 5 queries. In each query, Bob should give three integers t, l and r to Alice such that t is either 1 or 2, and 1≤l≤r≤n. Alice will then tell Bob the value equal to $$\min_{i=l}^{r} a[p_{t,i}].$$
Note that all rounds are independent of each other. In particular, the values of a, u and v can be different in different rounds.
Bob is puzzled as he only knows the HLD solution, which requires O(log(n)) queries per round. So he needs your help to win the game.
† The MEX (minimum excludant) of a collection of integers c1,c2,…,ck is defined as the smallest non-negative integer x which does not occur in the collection c.
Interaction
Each test contains multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. Read it. The description of the test cases follows.
The first line of each test case contains two positive integers n and q (2≤n≤105, 1≤q≤104) — the number of nodes in T and the number of rounds respectively.
The following next n−1 lines contains two integers u and v (1≤u,v≤n, u=v) — denoting an edge between nodes u and v. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of n and q over all test cases does not exceed 105 and 104 respectively.
It is also guaranteed that the sum of n⋅q does not exceed 3⋅106.
The interaction for each test case begins by outputting two permutations p1 and p2 of [1,2,…,n].
On a new line, output n space-separated distinct integers denoting p1.
In the next line, output n space-separated distinct integers denoting p2.
Alice will start playing the game.
For each round, you must read two integers, u and v (1≤u,v≤n, u=v). You need to find the MEX of the values on the unique simple path between nodes u and v.
To make a query, output "? t l r" without quotes, such that t is either 1 or 2, and 1≤l≤r≤n. Afterwards, you should read a single integer — the answer to your query mini=lrapt,i. You can make at most 5 such queries in each round.
If you want to print the answer, output "! x" (1≤x,y≤n) without quotes. After doing that, read a single integer, which is normally equal to 1.
If you receive the integer −1 instead of a valid reply, it means your program has made an invalid query, exceeded the query limit, or gave 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.
After printing a query or the answer, do not forget to output the end of the 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, follow the test format below.
The first line should contain a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case should contain two positive integers n and q (2≤n≤105; 1≤q≤104) — the number of nodes in T and the number of rounds respectively.
The following next n−1 lines should contain two integers u and v (1≤u,v≤n, u=v) — denoting an edge between nodes u and v. The given edges must form a tree.
For each of the q rounds, first print a permutation of [0,1,2,…,n−1] on a new line, denoting the array a chosen by Alice during the start of the round.
In the following line, print two distinct nodes u and v (1≤u,v≤v, u=v), representing the endpoints of the path asked by Alice.
The sum of n and q over all test cases should not exceed 105 and 104 respectively.
The sum of n⋅q should not exceed 3⋅106.
这是一个交互式问题。
爱丽丝有一棵包含 n 个节点的树 T,节点编号为 1 到 n。爱丽丝会将 T 展示给鲍勃。在观察完 T 后,鲍勃需要告诉爱丽丝两个排列 p1 和 p2,它们均为 [1,2,…,n] 的排列。
随后,爱丽丝将进行 q 轮如下游戏:
- 爱丽丝将构造一个数组 a,它是 [0,1,…,n−1] 的一个排列;节点 v 的值即为 av。
- 爱丽丝将在 T 中选择两个节点 u 和 v(满足 1≤u,v≤n 且 u=v),并将它们告知鲍勃。鲍勃需要求出节点 u 与 v 之间唯一简单路径上所有节点取值的 MEX†。
- 为求得该值,鲍勃每轮最多可向爱丽丝提出 5 次询问。每次询问中,鲍勃需向爱丽丝提供三个整数 t、l 和 r,其中 t 只能是 1 或 2,且满足 1≤l≤r≤n。爱丽丝将返回以下值:
i=lminra[pt,i].
注意:所有轮次相互独立。特别地,不同轮次中数组 a、节点 u 和 v 的取值均可不同。
鲍勃感到困惑,因为他只知道基于重链剖分(HLD)的解法,而该解法每轮需 O(log(n)) 次询问。因此他需要你的帮助来赢得这场游戏。
† 一组整数 c1,c2,…,ck 的 MEX(最小未出现非负整数)定义为未出现在该集合 c 中的最小非负整数 x。
交互方式
每个测试点包含多个测试用例。第一行是一个整数 t(1≤t≤104),表示测试用例数量。请读入该值。随后是各测试用例的描述。
每个测试用例的第一行包含两个正整数 n 和 q(2≤n≤105,1≤q≤104),分别表示树 T 的节点数和游戏轮数。
接下来 n−1 行,每行包含两个整数 u 和 v(1≤u,v≤n,u=v),表示节点 u 与 v 之间存在一条边。保证所给边构成一棵树。
保证所有测试用例中 n 的总和不超过 105,q 的总和不超过 104。
同时保证所有测试用例中 n⋅q 的总和不超过 3⋅106。
每个测试用例的交互以输出两个排列 p1 和 p2(均为 [1,2,…,n] 的排列)开始。
在新的一行中,输出 n 个以空格分隔的互不相同的整数,表示 p1。
在下一行中,输出 n 个以空格分隔的互不相同的整数,表示 p2。
此后爱丽丝开始进行游戏。
对每一轮,你必须读入两个整数 u 和 v(满足 1≤u,v≤n 且 u=v)。你需要求出节点 u 与 v 之间唯一简单路径上所有节点取值的 MEX。
为发起一次询问,请输出 ? t l r(不含引号),其中 t 为 1 或 2,且 1≤l≤r≤n。随后,你应读入一个整数 —— 即你所提询问的答案 mini=lrapt,i。每轮最多可进行 5 次此类询问。
若你想输出答案,请输出 ! x(不含引号)(注意:此处原文有误,应为 ! x,而非 ! x y;且 x 应为非负整数,无 1≤x,y≤n 的限制)。输出后,请读入一个整数,该整数通常等于 1。
若你收到的整数为 −1 而非有效回复,则说明你的程序发出了非法询问、超出询问次数限制,或在上一测试用例中给出了错误答案。此时你的程序必须立即终止,以获得“Wrong Answer”判决。否则,由于你的程序将继续从已关闭的流中读取数据,你可能会得到任意判决。
在输出一次询问或答案后,切勿忘记输出换行符并刷新输出缓冲区。否则你会收到 “Idleness limit exceeded”。为此,请使用:
- C++ 中:
fflush(stdout)或cout.flush(); - Java 中:
System.out.flush(); - Pascal 中:
flush(output); - Python 中:
stdout.flush(); - 其他语言请参阅相应文档。
Hack 方法
要 hack 本题,请按如下格式构造测试数据。
第一行应为一个整数 t(1≤t≤104),表示测试用例数量。
每个测试用例的第一行应包含两个正整数 n 和 q(2≤n≤105;1≤q≤104),分别表示树 T 的节点数和游戏轮数。
接下来 n−1 行,每行包含两个整数 u 和 v(1≤u,v≤n,u=v),表示节点 u 与 v 之间存在一条边。所给边必须构成一棵树。
对 q 轮中的每一轮,首先在新一行输出一个 [0,1,2,…,n−1] 的排列,表示爱丽丝在该轮开始时选定的数组 a。
在下一行中,输出两个互异的节点 u 和 v(1≤u,v≤n,u=v),表示爱丽丝所询问路径的两个端点。
所有测试用例中 n 的总和与 q 的总和分别不应超过 105 和 104。
所有测试用例中 n⋅q 的总和不应超过 3⋅106。
输入输出样例
输入#1
1 3 1 1 2 2 3 2 3 1 0 1
输出#1
1 2 3 2 1 3 ? 1 2 3 ? 2 1 3 ! 0
说明/提示
In the first test, the interaction proceeds as follows.
Solution
Jury
Explanation
1
There are 1 test cases.
3 1
The tree T consists of 3 nodes, and Alice will play for only one round.
1 2
First edge of T
2 3
Second edge of T
1 2 3
The permutation p1
2 1 3
The permutation p2
Alice shuffles a to a=[0,2,1] before giving the nodes for the only round.
2 3
Nodes for the round
? 1 2 3
1
min(ap1,2,ap1,3)=min(a2,a3)=1
? 2 1 3
0
min(ap2,1,ap2,2,ap2,3)=min(a2,a1,a3)=0
! 0
1
Considering the output of queries, it is clear that MEX is 0. Since the output is correct, the jury responds with 1.
After each test case, make sure to read 1 or −1.
在第一个测试用例中,交互过程如下所示。
解答
裁判
说明
1
共有 1 个测试用例。
3 1
树 T 包含 3 个节点,且 Alice 仅进行一轮游戏。
1 2
树 T 的第一条边
2 3
树 T 的第二条边
1 2 3
排列 p1
2 1 3
排列 p2
Alice 在向该轮提供节点前,将数组 a 置换为 a=[0,2,1]。
2 3
该轮所用的节点
? 1 2 3
1
min(ap1,2,ap1,3)=min(a2,a3)=1
? 2 1 3
0
min(ap2,1,ap2,2,ap2,3)=min(a2,a1,a3)=0
! 0
1
根据查询输出结果,显然 MEX 为 0。由于输出正确,裁判回应 1。
每个测试用例结束后,请务必读取 1 或 −1。
输入解题思路,AI测评打分。不知道怎么写?