CF1919H.Tree Diameter

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There is a hidden tree with nn vertices. The n−1n-1 edges of the tree are numbered from 11 to n−1n-1. You can ask the following queries of two types:

  1. Give the grader an array aa with n−1n - 1 positive integers. For each edge from 11 to n−1n - 1, the weight of edge ii is set to aia_i. Then, the grader will return the length of the diameter†^\dagger.
  2. Give the grader two indices 1≤a,b≤n−11 \le a, b \le n - 1. The grader will return the number of edges between edges aa and bb. In other words, if edge aa connects uau_a and vav_a while edge bb connects ubu_b and vbv_b, the grader will return min⁡(dist(ua,ub),dist(va,ub),dist(ua,vb),dist(va,vb))\min(\text{dist}(u_a, u_b), \text{dist}(v_a, u_b), \text{dist}(u_a, v_b), \text{dist}(v_a, v_b)), where dist(u,v)\text{dist}(u, v) represents the number of edges on the path between vertices uu and vv.

Find any tree isomorphic‡^\ddagger to the hidden tree after at most nn queries of type 1 and nn queries of type 2 in any order.

†^\dagger The distance between two vertices is the sum of the weights on the unique simple path that connects them. The diameter is the largest of all those distances.

‡^\ddagger Two trees, consisting of nn vertices each, are called isomorphic if there exists a permutation pp containing integers from 11 to nn such that edge (uu, vv) is present in the first tree if and only if the edge (pup_u, pvp_v) is present in the second tree.

存在一棵隐藏的、包含 nn 个顶点的树。该树的 n−1n-1 条边被编号为 11 到 n−1n-1。你可以提出以下两类查询:

  1. 向评测程序提供一个长度为 n−1n - 1 的正整数数组 aa。对于每条从 11 到 n−1n - 1 的边,将边 ii 的权重设为 aia_i。随后,评测程序将返回该加权树的直径†^\dagger 的长度。
  2. 向评测程序提供两个下标 1≤a,b≤n−11 \le a, b \le n - 1。评测程序将返回边 aa 与边 bb 之间的边数。换言之,若边 aa 连接顶点 uau_a 和 vav_a,边 bb 连接顶点 ubu_b 和 vbv_b,则评测程序将返回

    min⁡(dist(ua,ub), dist(va,ub), dist(ua,vb), dist(va,vb)),\min(\text{dist}(u_a, u_b),\ \text{dist}(v_a, u_b),\ \text{dist}(u_a, v_b),\ \text{dist}(v_a, v_b)),

    其中 dist(u,v)\text{dist}(u, v) 表示顶点 uu 与 vv 之间路径上的边数。

在至多进行 nn 次第 1 类查询和 nn 次第 2 类查询(顺序任意)的前提下,找出任意一棵与隐藏树同构‡^\ddagger 的树。

†^\dagger 两顶点间的距离定义为连接它们的唯一简单路径上所有边的权重之和;直径即为所有顶点对间距离的最大值。

‡^\ddagger 若两棵各有 nn 个顶点的树满足:存在一个由 11 到 nn 的整数构成的排列 pp,使得第一棵树中存在边 (u,v)(u, v) 当且仅当第二棵树中存在边 (pu,pv)(p_u, p_v),则称这两棵树同构。

输入格式

The first and only line of input contains a single integer nn (3≤n≤10003 \le n \le 1000) — the number of vertices in the tree.

输入仅有一行,包含一个整数 nn(3≤n≤10003 \le n \le 1000)——树中顶点的数量。

输入输出样例

  • 输入#1

    5
    
    3
    
    1
    
    9
    
    0

    输出#1

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

说明/提示

The hidden tree in the example is shown above. The number on the vertices represents the vertex number while the number on the edges represents the edge number.

In the first query, all the edges are set to weight 11, so the diameter has length 33 as shown in the diagram.

In the second query, there is 11 edge between edges 11 and 33.

In the third query, the diameter is 99 by taking edges 11, 22 and 33.

In the fourth query, there are no edges between edges 44 and 22.

The answer given in the example is shown in the above diagram. Since it is isomorphic to the hidden tree, it is accepted as a correct answer. Note that the edges can be printed in any order.

示例中隐藏的树如上图所示。顶点上的数字表示顶点编号,边上的数字表示边编号。

在第一次查询中,所有边的权重均设为 11,因此直径长度为 33,如图所示。

在第二次查询中,边 11 与边 33 之间存在 11 条边。

在第三次查询中,选取边 11、22 和 33 可使直径达到 99。

在第四次查询中,边 44 与边 22 之间不存在边。

示例中给出的答案如上图所示。由于该树与隐藏树同构,因此被接受为正确答案。注意:边的输出顺序可以任意。

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

首页