Intersecting Subtrees

题意翻译

### 题目描述 这是一道交互题 你和$Li\ Chen$正在玩一个奇怪的游戏。给出一棵$N$个点的树,双方分别给顶点编号为$1$到$N$,双方都不知道对方给树编号的方式。 接着双方在自己对应的树上选择一个联通子图,在**你的编号方式对应的树上**你选择了$x_1,x_2,...,x_{k_1}$,**在$Li\ Chen$的编号方式对应的树上**$Li\ Chen$选择了$y_1,y_2,...,y_{k_2}$,双方都知道$x_1,...,x_{k_1}$与$y_1,...,y_{k_2}$的值 现在你想知道两个子图是否存在至少一个公共点。你可以进行询问,问题有以下两种: $A \ x$:得到在你的编号方式下的$x$号点在$Li\ Chen$的编号方式下的值 $B \ x$:得到在$Li\ Chen$的编号方式下的$x$号点在你的编号方式下的值 现在请你使用**不多于$5$次**询问得出是否存在公共点,或者确定两棵子树没有公共点。 ### 输入数据 第一行一个正整数$t(1 \leq t \leq 100)$表示数据组数 在每一组数据交互开始之前,第一行一个正整数$n(1 \leq n \leq 1000)$表示树上的点数,接着$n-1$行每行两个正整数$u,v(1 \leq u,v \leq n)$表示**在你的编号方式下**编号为$u,v$的两个点之间有一条边。 接下来一行一个正整数$k_1(1 \leq k_1 \leq n)$表示你选出来的点的个数,下一行$k_1$个整数$x_1,...,x_{k_1}(1 \leq x_i \leq n)$描述你选出来的点在你的编号方式下的编号。 接下来一行一个正整数$k_2(1 \leq k_2 \leq n)$表示$Li\ Chen$选出来的点的个数,下一行$k_2$个整数$y_1,...,y_{k_2}(1 \leq y_i \leq n)$描述$Li\ Chen$选出来的点在$Li\ Chen$的编号方式下的编号。 数据会一组一组给出,所以请在上一组数据完成交互之后,再进行下一组数据的读入。 ### 交互实现 在标准输入输出中进行询问和回答。每一组数据可以提出不多于$5$个问题,形式如“题目描述”所述。 输出答案的格式为$C\ x$,其中$x$为你认为的公共的点**在你的编号方式对应的树上**的编号。如果你认为没有公共点,那么$x=-1$。 每输出一行,请$flush$你的输出,否则会返回$Idleness\ limit\ exceeded$。$flush$输出可以使用以下函数: $C++:$fflush(stdout); $Java:$System.out.flush(); $Python:$stdout.flush(); $Pascal:$flush(output); 其他语言见对应语言的函数声明 在任何时候如果输入了$-1$,表明出现了错误,请立即结束程序,此时会返回$Wrong\ answer$。如果你忽略了这个信息,由于你在一个关闭了的输入流中输入信息,可能会出现不可预料的错误。

题目描述

You are playing a strange game with Li Chen. You have a tree with $ n $ nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the vertices from $ 1 $ to $ n $ . Neither of you know the other's labelling of the tree. You and Li Chen each chose a subtree (i.e., a connected subgraph) in that tree. Your subtree consists of the vertices labeled $ x_1, x_2, \ldots, x_{k_1} $ in your labeling, Li Chen's subtree consists of the vertices labeled $ y_1, y_2, \ldots, y_{k_2} $ in his labeling. The values of $ x_1, x_2, \ldots, x_{k_1} $ and $ y_1, y_2, \ldots, y_{k_2} $ are known to both of you. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1044B/f8d6eab9a505e94e56f3fbc02db7e9ddfae952fb.png) The picture shows two labelings of a possible tree: yours on the left and Li Chen's on the right. The selected trees are highlighted. There are two common nodes.You want to determine whether your subtrees have at least one common vertex. Luckily, your friend Andrew knows both labelings of the tree. You can ask Andrew at most $ 5 $ questions, each of which is in one of the following two forms: - A x: Andrew will look at vertex $ x $ in your labeling and tell you the number of this vertex in Li Chen's labeling. - B y: Andrew will look at vertex $ y $ in Li Chen's labeling and tell you the number of this vertex in your labeling. Determine whether the two subtrees have at least one common vertex after asking some questions. If there is at least one common vertex, determine one of your labels for any of the common vertices.

输入输出格式

输入格式


输出格式


Each test consists of several test cases. The first line of input contains a single integer $ t $ ( $ 1 \leq t \leq 100 $ ) — the number of test cases. For each testcase, your program should interact in the following format. The first line contains a single integer $ n $ ( $ 1 \leq n \leq 1\,000 $ ) — the number of nodes in the tree. Each of the next $ n-1 $ lines contains two integers $ a_i $ and $ b_i $ ( $ 1\leq a_i, b_i\leq n $ ) — the edges of the tree, indicating an edge between node $ a_i $ and $ b_i $ according to your labeling of the nodes. The next line contains a single integer $ k_1 $ ( $ 1 \leq k_1 \leq n $ ) — the number of nodes in your subtree. The next line contains $ k_1 $ distinct integers $ x_1,x_2,\ldots,x_{k_1} $ ( $ 1 \leq x_i \leq n $ ) — the indices of the nodes in your subtree, according to your labeling. It is guaranteed that these vertices form a subtree. The next line contains a single integer $ k_2 $ ( $ 1 \leq k_2 \leq n $ ) — the number of nodes in Li Chen's subtree. The next line contains $ k_2 $ distinct integers $ y_1, y_2, \ldots, y_{k_2} $ ( $ 1 \leq y_i \leq n $ ) — the indices (according to Li Chen's labeling) of the nodes in Li Chen's subtree. It is guaranteed that these vertices form a subtree according to Li Chen's labelling of the tree's nodes. Test cases will be provided one by one, so you must complete interacting with the previous test (i.e. by printing out a common node or $ -1 $ if there is not such node) to start receiving the next one. You can ask the Andrew two different types of questions. - You can print "A x" ( $ 1 \leq x \leq n $ ). Andrew will look at vertex $ x $ in your labeling and respond to you with the number of this vertex in Li Chen's labeling. - You can print "B y" ( $ 1 \leq y \leq n $ ). Andrew will look at vertex $ y $ in Li Chen's labeling and respond to you with the number of this vertex in your labeling. You may only ask at most $ 5 $ questions per tree. When you are ready to answer, print "C s", where $ s $ is your label of a vertex that is common to both subtrees, or $ -1 $ , if no such vertex exists. Printing the answer does not count as a question. Remember to flush your answer to start receiving the next test case. After printing a question do not forget to print 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. If the judge responds with $ -1 $ , it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive Wrong Answer; it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Hack Format To hack, use the following format. Note that you can only hack with one test case. The first line should contain a single integer $ t $ ( $ t=1 $ ). The second line should contain a single integer $ n $ ( $ 1 \leq n \leq 1\,000 $ ). The third line should contain $ n $ integers $ p_1, p_2, \ldots, p_n $ ( $ 1\leq p_i\leq n $ ) — a permutation of $ 1 $ to $ n $ . This encodes the labels that Li Chen chose for his tree. In particular, Li Chen chose label $ p_i $ for the node you labeled $ i $ . Each of the next $ n-1 $ lines should contain two integers $ a_i $ and $ b_i $ ( $ 1\leq a_i, b_i\leq n $ ). These edges should form a tree. The next line should contain a single integer $ k_1 $ ( $ 1 \leq k_1 \leq n $ ). The next line should contain $ k_1 $ distinct integers $ x_1,x_2,\ldots,x_{k_1} $ ( $ 1 \leq x_i \leq n $ ). These vertices should form a subtree. The next line should contain a single integer $ k_2 $ ( $ 1 \leq k_2 \leq n $ ). The next line should contain $ k_2 $ distinct integers $ y_1, y_2, \ldots, y_{k_2} $ ( $ 1 \leq y_i \leq n $ ). These vertices should form a subtree in Li Chen's tree according to the permutation above.

输入输出样例

输入样例 #1

1
3
1 2
2 3
1
1
1
2
2
1

输出样例 #1

A 1
B 2
C 1

输入样例 #2

2
6
1 2
1 3
1 4
4 5
4 6
4
1 3 4 5
3
3 5 2
3
6
1 2
1 3
1 4
4 5
4 6
3
1 2 3
3
4 1 6
5

输出样例 #2

B 2
C 1
A 1
C -1

说明

For the first sample, Li Chen's hidden permutation is $ [2, 3, 1] $ , and for the second, his hidden permutation is $ [5, 3, 2, 4, 1, 6] $ for both cases. In the first sample, there is a tree with three nodes in a line. On the top, is how you labeled the tree and the subtree you chose, and the bottom is how Li Chen labeled the tree and the subtree he chose: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1044B/20da1d3951c06c9ea90b744aab9f3033dac72c43.png)In the first question, you ask Andrew to look at node $ 1 $ in your labelling and tell you the label of it in Li Chen's labelling. Andrew responds with $ 2 $ . At this point, you know that both of your subtrees contain the same node (i.e. node $ 1 $ according to your labeling), so you can output "C 1" and finish. However, you can also ask Andrew to look at node $ 2 $ in Li Chen's labelling and tell you the label of it in your labelling. Andrew responds with $ 1 $ (this step was given with the only reason — to show you how to ask questions). For the second sample, there are two test cases. The first looks is the one from the statement: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1044B/f8d6eab9a505e94e56f3fbc02db7e9ddfae952fb.png)We first ask "B 2", and Andrew will tell us $ 3 $ . In this case, we know $ 3 $ is a common vertex, and moreover, any subtree with size $ 3 $ that contains node $ 3 $ must contain node $ 1 $ as well, so we can output either "C 1" or "C 3" as our answer. In the second case in the second sample, the situation looks as follows: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1044B/90d5b00e08570be05b2931735d98bbcd805bf31f.png)In this case, you know that the only subtree of size $ 3 $ that doesn't contain node $ 1 $ is subtree $ 4,5,6 $ . You ask Andrew for the label of node $ 1 $ in Li Chen's labelling and Andrew says $ 5 $ . In this case, you know that Li Chen's subtree doesn't contain node $ 1 $ , so his subtree must be consist of the nodes $ 4,5,6 $ (in your labelling), thus the two subtrees have no common nodes.