CF1174F.Ehab and the Big Finale

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is an interactive problem.

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

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

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

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

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

这是一个交互式问题。

你将得到一棵包含 nn 个节点的树,根节点为节点 11。树是一种无环的连通图。

我们选定一个隐藏节点 xx。为了找出该节点,你可以提出两类查询:

  • d u(其中 1≤u≤n1 \le u \le n):我们将返回节点 uu 与节点 xx 之间的距离。两个节点间的距离定义为它们之间最短路径所含边的数量。
  • s u(其中 1≤u≤n1 \le u \le n):我们将返回从节点 uu 到节点 xx 的路径上的第二个节点。但这里有一个转折:若 uu 不是 xx 的祖先,则你会收到 “Wrong answer”(答案错误)判据!

节点 aa 被称为节点 bb 的祖先,当且仅当 a≠ba \ne b,且从根节点 11 到节点 bb 的最短路径经过节点 aa。注意,在本题中,一个节点不被视为自身的祖先。

你能否在不超过 3636 次查询内确定 xx?在每个测试用例中,隐藏节点 xx 在开始时即已固定,且不随你的查询而改变。

输入格式

The first line contains the integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the number of nodes in the tree.

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

第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)——树中节点的数量。

接下来的 n−1n-1 行每行包含两个用空格分隔的整数 uu 和 vv(1≤u,v≤n1 \le u,v \le n),表示节点 uu 与节点 vv 之间存在一条边。保证所给图是一棵树。

输出格式

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

要输出答案,请输出“! x”(不含引号)。

输入输出样例

  • 输入#1

    5
    1 2
    1 3
    3 4
    3 5
    3
    5

    输出#1

    d 2
    s 3
    ! 5

说明/提示

In the first example, the hidden node is node 55.

We first ask about the distance between node xx and node 22. The answer is 33, so node xx is either 44 or 55. We then ask about the second node in the path from node 33 to node xx. Note here that node 33 is an ancestor of node 55. We receive node 55 as the answer. Finally, we report that the hidden node is node 55.

在第一个例子中,隐藏节点是节点 55。

我们首先询问节点 xx 与节点 22 之间的距离。回答为 33,因此节点 xx 要么是 44,要么是 55。接着,我们询问从节点 33 到节点 xx 的路径上的第二个节点。注意,此处节点 33 是节点 55 的祖先。我们得到的回答是节点 55。最后,我们报告隐藏节点为节点 55。

输入解题思路,AI测评打分。不知道怎么写?

首页