Where Is the Root?

Time limit: 0.1s Memory limit: 256MB Input: Output:

Task

You are given a tree of nn vertices. The tree is a graph such that there is exactly one simple path between every pair of vertices. It's also guaranteed that at least one vertex is directly connected by an edge to at least 33 vertices. One of the vertices is the root, and your task is to find it. In order to do this, you are allowed to ask queries of the following form:

  • For a given set a1,a2,,ama_1, a_2,\dots, a_m of vertices, check if their lowest common ancestor is in this set.

A vertex vv is a common ancestor of a set SS of vertices if the paths from all vertices in SS to the root pass through vv. The lowest common ancestor (LCA) of a set SS of vertices is the common ancestor of SS which is farthest from the root.

Interaction protocol

You must implement one function:

void solve(int n, vector<pair<int,int>> V);

You can call the following functions:

bool query(vector<int> a);
void check(int u);

The function solve will be called exactly once at the beginning of the interaction with two parameters:

  • nn: the number of nodes
  • VV: the edges of the given tree.

The function query may be called by the contestant no more than 1 0001\ 000 times. It will return a boolean value true if the LCA (Lowest Common Ancestor) of the nodes is in the given set, and false otherwise.
To check if a node is the root, you can call the check function. After that, the interaction will terminate, and you can't ask any more queries or check for other nodes.
Atention! You must not implement the main function, and must #include the root.h header! You can use global variables and other functions.

Constraints and clarifications

  • In the first and second subtask you can ask at most 1 0001\ 000 queries.
  • In the third subtask, let kk be the maximum number of queries you asked in any test. If k9k \leq 9, you will receive all the points for this subtask (8383 points). Otherwise, you will get max(10,83(1ln(k6)7)) \displaystyle \lfloor \max(10, 83 \cdot (1- \frac{\ln(k-6)}{7})) \rfloor points.
# Points Constraints
1 7 n9n \leq 9
2 10 n30n \leq 30
3 83 n500n \leq 500

Example

  • Grader runs solve(7, {{4,1},{1,2},{4,3},{3,5},{3,6},{4,7}});.
  • User calls query({2, 5, 6}); and grader returns false.
  • User calls query({3, 6, 3, 5}); and grader returns true.
  • User calls query({2, 1, 7}); and grader returns false.
  • User calls query({2, 4, 6}); and grader returns true.
  • User calls check(4); and the program ends successfully.

Explanation

The hidden root is vertex 44.
In the first query, the LCA of vertices 55 and 66 is vertex 33 which is not among vertices 55 and 66 so the answer is false.
In the second query, the LCA of vertices 33, 55, and 66 is vertex 33 so the answer is true.
In the third query, the LCA of vertices 11 and 77 is vertex 44 so the answer is false.
In the fourth query, the LCA of vertices 44 and 66 is vertex 44 so the answer is true.
After that, we can guess that root is vertex 44 which is the correct answer.

Log in or sign up to be able to send submissions!