Think like the best software engineer and give e the code in c++ for : This problem is interactive. Baudelaire is very rich, so he bought a tree of size n , rooted at some arbitrary node. Additionally, every node has a value of 1 or −1 . Cow the Nerd saw the tree and fell in love with it. However, computer science doesn't pay him enough, so he can't afford to buy it. Baudelaire decided to play a game with Cow the Nerd, and if he won, he would gift him the tree. Cow the Nerd does not know which node is the root, and he doesn't know the values of the nodes either. However, he can ask Baudelaire queries of two types: 1 u2 ... uk : Let f(u) be the sum of the values of all nodes in the path from the root of the tree to node u . Cow the Nerd may choose an integer k (1≤k≤n) and k nodes u1,u2,...,uk , and he will receive the value f(u1)+f(u2)+...+f(uk) . 2 u : Baudelaire will toggle the value of node u . Specifically, if the value of u is 1 , it will become −1 , and vice versa. Cow the Nerd wins if he guesses the value of every node correctly (the values of the final tree, after performing the queries) within n+200 total queries. Can you help him win? Input The first line of the input contains a single integer t (1≤t≤100) , the number of test cases. The first line of each test case contains a single integer n (2≤n≤103) , the size of the tree. Each of the next n−1 lines contains two integers u and v (1≤u,v≤n,u≠v) , denoting an edge between nodes u and v in the tree. It is guaranteed that the sum of n over all test cases does not exceed 103 and that each graph provided is a valid tree. Interaction To ask a query of type 1 , output a line in the following format (without the quotes): "? 1 k u1 u2 ... uk ", (1≤k,ui≤n ) The jury will return a single integer, f(u1)+f(u2)+...+f(uk) . To ask a query of type 2 , output a line in the following format: "? 2 u " (1≤u≤n ) The jury will toggle the value of node u : if its value is 1 , it will become −1 and vice versa. When you have found the answer, output a single line in the following format: "! v1,v2,...,vn " (vi=1 or vi=−1 , and vi is the value of node i after performing the queries) After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query. The interactor is not adaptive, meaning that the values of the tree are known before the participant asks the queries. If your program makes more than n+200 queries, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get the Idleness Limit Exceeded verdict. 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 the documentation for other languages. Hacks For hacks, use the following format. The first line should contain a single integer t (1≤t≤100) — the number of test cases. The first line of each test case must contain exactly two integers n and root (2≤n≤103,1≤root≤n) — the size of the tree and the root of the tree. The second line of each test case must contain exactly n integers a1,a2,...,an (|ai|=1) — where ai is the value of node i . Each of the following n−1 lines must contain exactly two integers u and v (1≤u,v≤n) — denoting an edge of the tree between nodes u and v . The sum of n over all test cases must not exceed 103 and every graph provided must be a valid tree.
Question:
Think like the best software engineer and give e the code in c++ for : This problem is interactive. Baudelaire is very rich, so he bought a tree of size n , rooted at some arbitrary node. Additionally, every node has a value of 1 or −1 . Cow the Nerd saw the tree and fell in love with it. However, computer science doesn't pay him enough, so he can't afford to buy it. Baudelaire decided to play a game with Cow the Nerd, and if he won, he would gift him the tree. Cow the Nerd does not know which node is the root, and he doesn't know the values of the nodes either. However, he can ask Baudelaire queries of two types: 1 u2 ... uk : Let f(u) be the sum of the values of all nodes in the path from the root of the tree to node u . Cow the Nerd may choose an integer k (1≤k≤n) and k nodes u1,u2,...,uk , and he will receive the value f(u1)+f(u2)+...+f(uk) . 2 u : Baudelaire will toggle the value of node u . Specifically, if the value of u is 1 , it will become −1 , and vice versa. Cow the Nerd wins if he guesses the value of every node correctly (the values of the final tree, after performing the queries) within n+200 total queries. Can you help him win? Input The first line of the input contains a single integer t (1≤t≤100) , the number of test cases. The first line of each test case contains a single integer n (2≤n≤103) , the size of the tree. Each of the next n−1 lines contains two integers u and v (1≤u,v≤n,u≠v) , denoting an edge between nodes u and v in the tree. It is guaranteed that the sum of n over all test cases does not exceed 103 and that each graph provided is a valid tree. Interaction To ask a query of type 1 , output a line in the following format (without the quotes): "? 1 k u1 u2 ... uk ", (1≤k,ui≤n ) The jury will return a single integer, f(u1)+f(u2)+...+f(uk) . To ask a query of type 2 , output a line in the following format: "? 2 u " (1≤u≤n ) The jury will toggle the value of node u : if its value is 1 , it will become −1 and vice versa. When you have found the answer, output a single line in the following format: "! v1,v2,...,vn " (vi=1 or vi=−1 , and vi is the value of node i after performing the queries) After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query. The interactor is not adaptive, meaning that the values of the tree are known before the participant asks the queries. If your program makes more than n+200 queries, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get the Idleness Limit Exceeded verdict. 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 the documentation for other languages. Hacks For hacks, use the following format. The first line should contain a single integer t (1≤t≤100) — the number of test cases. The first line of each test case must contain exactly two integers n and root (2≤n≤103,1≤root≤n) — the size of the tree and the root of the tree. The second line of each test case must contain exactly n integers a1,a2,...,an (|ai|=1) — where ai is the value of node i . Each of the following n−1 lines must contain exactly two integers u and v (1≤u,v≤n) — denoting an edge of the tree between nodes u and v . The sum of n over all test cases must not exceed 103 and every graph provided must be a valid tree.
Asked by: Test User 100003
Created at: 2025-04-25 14:13:44
Final Answer:
📢 Instructions:
- Ask anything relatd to above question only.
- For new Question goto :
VIPSolutions