Codeforces LATOKEN Round 1 (Div. 1 + Div. 2)
H
Lost Nodes
Codeforces
3500
原题
← G→
翻译中…

This is an interactive problem.

翻译中…

As he qualified for IOI this year, Little Ericyi was given a gift from all his friends: a tree of $n$ nodes!

翻译中…

On the flight to IOI Little Ericyi was very bored, so he decided to play a game with Little Yvonne with his new tree. First, Little Yvonne selects two (not necessarily different) nodes $a$ and $b$ on the tree (without telling Ericyi), and then gives him a hint $f$ (which is some node on the path from $a$ to $b$).

翻译中…

Then, Little Ericyi is able to ask the following question repeatedly:

翻译中…
  • If I rooted the tree at node $r$ (Ericyi gets to choose $r$), what would be the Lowest Common Ancestor of $a$ and $b$?
翻译中…

Little Ericyi's goal is to find the nodes $a$ and $b$, and report them to Little Yvonne.

翻译中…

However, Little Yvonne thought this game was too easy, so before he gives the hint $f$ to Little Ericyi, he also wants him to first find the maximum number of queries required to determine $a$ and $b$ over all possibilities of $a$, $b$, and $f$ assuming Little Ericyi plays optimally. Little Ericyi defines an optimal strategy as one that makes the minimum number of queries. Of course, once Little Ericyi replies with the maximum number of queries, Little Yvonne will only let him use that many queries in the game.

翻译中…

The tree, $a$, $b$, and $f$ are all fixed before the start of the game and do not change as queries are made.

翻译中…

Interaction

翻译中…

First read a line containing the integer $n$ ($1 \le n \le 10^5$), the number of nodes in the tree.

翻译中…

The next $n−1$ lines describe Little Ericyi's tree. These lines contain two integers $u$ and $v$ ($1 \le u,v \le n$) denoting an edge between $u$ and $v$ ($u \neq v$). It is guaranteed that these edges form a tree.

翻译中…

After that you should output $k$, the maximum number of queries needed to determine $a$ and $b$ over all possibilities of $a$, $b$, and $f$ assuming Little Ericyi plays optimally. You should output end of line and flush the output after printing $k$.

翻译中…

After that read a line containing the integer $f$ ($1 \le f \le n$) — the hint: a node on the path from $a$ to $b$, inclusive.

翻译中…

After that, you can start making queries. You will be limited to making at most $k$ queries, where $k$ is the number you printed.

翻译中…

Each query is made in the format "? r", where $r$ is an integer $1 \le r \le n$ denoting the root node you want for the query.

翻译中…

You will then receive an integer $x$ ($1 \le x \le n$), the Lowest Common Ancestor of $a$ and $b$ if the tree was rooted at $r$.

翻译中…

When your program has found the nodes $a$, $b$, report the answer in the following format: "! a b", where $a$ and $b$ are the two hidden nodes and terminate your program normally immediately after flushing the output stream. You may output $a$ and $b$ in any order.

翻译中…

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 documentation for other languages.
翻译中…

If at any point you make an invalid output or make more than $k$ queries, the interaction will terminate and you will receive a Wrong Answer verdict. An invalid output is defined as either an invalid query or a value of $k$ less than $0$ or greater than $n$.

翻译中…

Hacks

翻译中…

To hack a solution, use the following format:

翻译中…

The first line contains the integer $n$ ($1 \le n \le 10^5$).

翻译中…

The next $n−1$ lines contain two integers $u$ and $v$ ($1 \le u,v \le n$) denoting an edge between $u$ and $v$ ($u \neq v$). These $n-1$ edges must form a tree.

翻译中…

The next line of input contains the nodes $a$ and $b$ ($1 \le a,b \le n$), separated by a space.

翻译中…

The final line of input contains the integer $f$ ($1 \le f \le n$). Node $f$ should be on the simple path from $a$ to $b$ (inclusive).

翻译中…

Note

翻译中…

Here is the the tree from the first sample interaction. Nodes $a$ and $b$ are highlighted.

翻译中…

image

翻译中…

Notice that $a$ and $b$ can be output in any order.

翻译中…

Additionally, here are the answers to querying every single node $1,2,\ldots,n$ for your convenience:

翻译中…
  • $1$: $1$
  • $2$: $2$
  • $3$: $2$
  • $4$: $4$
翻译中…

翻译中…

Here is the the tree from the second sample interaction. Again, nodes $a$ and $b$ are highlighted.

翻译中…

image

翻译中…

Lastly, here are the answers to querying every single node $1,2,\ldots,n$ (in example $2$) for your convenience:

翻译中…
  • $1$: $1$
  • $2$: $4$
  • $3$: $1$
  • $4$: $4$
  • $5$: $4$
翻译中…

样例

Input #1
4
3 2
2 1
2 4

1

1

2

2
Output #1
3

? 1

? 2

? 3

! 4 1
InputCopy OutputCopy
Input #2
5
3 1
1 4
4 5
4 2

1

4

1

4
Output #2
3

? 4

? 1

? 5

! 1 4
InputCopy OutputCopy
题目信息
题号
1534H
时间限制
4 s
内存限制
256 MB
标签
构造
动态规划
图论
交互题
排序
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。