CF2193G.Paths in a Tree

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is an interactive problem.

In this interactive problem, you are given an acyclic, connected, undirected graph consisting of nn vertices. We define a path between two vertices vv and uu as a sequence of distinct vertices p1,p2,…pkp_1, p_2,\dots p_k such that p1=vp_1 = v, pk=up_k = u, and for all ii (1≤i<k1 \le i \lt k), there exists an edge between vertices pip_i and pi+1p_{i+1}.

There are hidden vertices xx and yy (they may coincide). You can make the following queries:

  • Choose two vertices aa, bb (1≤a,b≤n1\le a, b\le n). The jury will respond with 11 if the path between vertices xx, yy and the path between vertices aa, bb share at least one common vertex, and will respond with 00 otherwise.

Your task is to find at least one vertex on the path between xx and yy in no more than ⌊n2⌋+1\lfloor\frac{n}{2}\rfloor + 1 queries.

Note that the interactor is adaptive, which means that the hidden vertices may change depending on your queries, but will not contradict previous queries.

这是一个交互式问题。

在本交互式问题中,你将得到一个由 nn 个顶点构成的无环、连通、无向图。我们定义两个顶点 vv 和 uu 之间的一条路径为一个由互不相同的顶点组成的序列 p1,p2,…pkp_1, p_2,\dots p_k,满足 p1=vp_1 = v,pk=up_k = u,且对所有 ii(1≤i<k1 \le i < k),顶点 pip_i 与 pi+1p_{i+1} 之间存在一条边。

存在两个隐藏顶点 xx 和 yy(它们可能重合)。你可以进行如下查询:

  • 选择两个顶点 aa、bb(1≤a,b≤n1\le a, b\le n)。评测系统将返回 11,当且仅当顶点 xx 与 yy 之间的路径和顶点 aa 与 bb 之间的路径至少有一个公共顶点;否则返回 00。

你的任务是在至多 ⌊n2⌋+1\lfloor\frac{n}{2}\rfloor + 1 次查询内,找出 xx 与 yy 之间路径上的至少一个顶点。

注意:该交互器是自适应的,这意味着隐藏顶点可能根据你的查询而改变,但其变化不会与之前的所有查询结果产生矛盾。

输入格式

Each test consists of several test cases. The first line contains one integer tt (1≤t≤1041\le t\le 10^4) — the number of test cases. The following lines describe the test cases.

The first line of each test case contains one integer nn (2≤n≤2⋅1052\le n\le 2\cdot 10^5) — the number of vertices in the graph.

Next, there are n−1n - 1 lines, each containing two integers vv, uu (1≤v,u≤n1\le v, u\le n), indicating that vertices vv and uu are connected by an edge in the graph.

It is guaranteed that the sum of nn across all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含若干个测试用例。第一行包含一个整数 tt(1≤t≤1041\le t\le 10^4),表示测试用例的数量。接下来的行描述各个测试用例。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052\le n\le 2\cdot 10^5),表示图中顶点的数量。

随后是 n−1n - 1 行,每行包含两个整数 vv 和 uu(1≤v,u≤n1\le v, u\le n),表示顶点 vv 和 uu 在图中由一条边相连。

保证所有测试用例的 nn 值之和不超过 2⋅1052\cdot 10^5。

输入输出样例

  • 输入#1

    3
    
    2
    1 2
    
    1
    
    
    3
    1 2
    1 3
    
    0
    
    0
    
    
    4
    1 2
    2 3
    2 4
    
    0
    
    1

    输出#1

    ? 1 1
    
    ! 1
    
    
    ? 1 1
    
    ? 2 2
    
    ! 3
    
    
    ? 1 3
    
    ? 4 4
    
    ! 4

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

首页