Ehab and the Big Finale
Contest: Codeforces Round 563 (Div. 2)
Difficulty rating: 2400
Tags: constructive algorithms, divide and conquer, graphs, implementation, interactive, trees

This is an interactive problem.

You're given a tree consisting of $$$n$$$ nodes, rooted at node $$$1$$$. A tree is a connected graph with no cycles.

We chose a hidden node $$$x$$$. In order to find this node, you can ask queries of two types:

- d $$$u$$$ ($$$1 \le u \le n$$$). We will answer with the distance between nodes $$$u$$$ and $$$x$$$. The distance between two nodes is the number of edges in the shortest path between them.
- s $$$u$$$ ($$$1 \le u \le n$$$). We will answer with the second node on the path from $$$u$$$ to $$$x$$$. However, there's a plot twist. If $$$u$$$ is not an ancestor of $$$x$$$, you'll receive "Wrong answer" verdict!

Node $$$a$$$ is called an ancestor of node $$$b$$$ if $$$a \ne b$$$ and the shortest path from node $$$1$$$ to node $$$b$$$ passes through node $$$a$$$. Note that in this problem a node is not an ancestor of itself.

Can you find $$$x$$$ in no more than $$$36$$$ queries? The hidden node is fixed in each test beforehand and does not depend on your queries.

Input format:
The first line contains the integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of nodes in the tree.

Each of the next $$$n-1$$$ lines contains two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$) that mean there's an edge between nodes $$$u$$$ and $$$v$$$. It's guaranteed that the given graph is a tree.

Output format:
To print the answer, print "! x" (without quotes).

Interaction format:
To ask a question, print it in one of the formats above:

- d $$$u$$$ ($$$1 \le u \le n$$$), or
- s $$$u$$$ ($$$1 \le u \le n$$$).

After each question, you should read the answer: either the distance or the second vertex on the path, as mentioned in the legend.

If we answer with $$$-1$$$ instead of a valid answer, that means you exceeded the number of queries, made an invalid query, or violated the condition in the second type of queries. Exit immediately after receiving $$$-1$$$ and you will see 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, 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 the documentation for other languages.

Hacks:

The first line should contain two integers $$$n$$$ and $$$x$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le x \le n$$$).

Each of the next $$$n-1$$$ lines should contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$) that mean there is an edge between nodes $$$u$$$ and $$$v$$$. The edges must form a tree.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example, the hidden node is node $$$5$$$.

We first ask about the distance between node $$$x$$$ and node $$$2$$$. The answer is $$$3$$$, so node $$$x$$$ is either $$$4$$$ or $$$5$$$. We then ask about the second node in the path from node $$$3$$$ to node $$$x$$$. Note here that node $$$3$$$ is an ancestor of node $$$5$$$. We receive node $$$5$$$ as the answer. Finally, we report that the hidden node is node $$$5$$$.

Example input:
5
1 2
1 3
3 4
3 5
3
5

Example output:
d 2
s 3
! 5

Editorial:
Let $$$dep_a$$$ be the depth of node $$$a$$$ and $$$sz_a$$$ be the size of the subtree of node $$$a$$$. First, we'll query the distance between node 1 and node $$$x$$$ to know $$$dep_x$$$. The idea in the problem is to maintain a "search scope", some nodes such that $$$x$$$ is one of them, and to try to narrow it down with queries. From this point, I'll describe two solutions:
HLD solution
Assume that your search scope is the subtree of some node $$$u$$$ (initially, $$$u$$$=1). How can we narrow it down efficiently? I'll pause here to add some definitions. The heavy child of a node $$$a$$$ is the child that has the maximal subtree size. The heavy path of node $$$a$$$ is the path that starts with node $$$a$$$ and every time moves to the heavy child of the current node. Now back to our algorithm. Let's get the heavy path of node $$$u$$$. Assume its other endpoint is node $$$v$$$. We know that a prefix of this path contains ancestors of node $$$x$$$. Let the deepest node in the path that is an ancestor of node $$$x$$$ be node $$$y$$$ (the last node in this prefix). I'll now add a drawing to help you visualize the situation.
So, recapping, $$$u$$$ is the root of your search scope, $$$v$$$ is the endpoint of the heavy path starting from $$$u$$$, $$$x$$$ is the hidden node, and $$$y$$$ the last ancestor of $$$x$$$ in the heavy path. Notice that $$$y$$$ is $$$lca(x,v)$$$. Now, we know that $$$dist(x,v)=dep_x+dep_v-2*dep_y$$$. Since we know $$$dep_x$$$, and we know $$$dep_v$$$, we can query $$$dist(x,v)$$$ to find $$$dep_y$$$. Since all nodes in the path have different depths, that means we know $$$y$$$ itself!
Observation #1: $$$dist(u,x)+dist(v,x)=dist(u,v)+2*dist(x,y)$$$. In both sides of the equation, every edge in the heavy path appears once, and every edge in the path from $$$x$$$ to $$$y$$$ appears twice.
We know $$$dist(u,x)=dep_x-dep_u$$$. We also know $$$dist(u,v)=dep_v-dep_u$$$. Additionally, we can query $$$dist(v,x)$$$. From these 3 pieces of information and the equation above, we can find $$$dist(x,y)$$$.
Observation #2: $$$dist(u,y)=dist(u,x)-dist(x,y)$$$. Now we know $$$dist(u,y)$$$. Since all nodes in the heavy path have different distances from node $$$u$$$, we know $$$y$$$ itself!
Now, if $$$dep_x=dep_y$$$, $$$x=y$$$, so we found the answer. Otherwise, we know, by definition, that $$$y$$$ is an ancestor of $$$x$$$, so it's safe to use the second query type. Let the answer be node $$$l$$$. We can repeat the algorithm with $$$u=l$$$! How long will this algorithm take? Note that $$$l$$$ can't be the heavy child of $$$y$$$ (because $$$y$$$ is the last ancestor of $$$x$$$ in the heavy path), so $$$sz_l \le \lfloor\frac{sz_y}{2} \rfloor$$$, since it's well-known that only the heavy child can break that condition. So with only 2 queries, we managed to cut down at least half of our search scope! So this algorithm does no more than 34 queries (actually 32 under these constraints, but that's just a small technicality).
Code link:
Query complexity: $$$2*\lfloor log(n) \rfloor$$$.