CF1712F.Triameter

NOI/NOI+/CTSC

通过率:0%

时间限制:4.50s

内存限制:768MB

AC君温馨提醒

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

题目描述

— What is my mission?

— To count graph diameters.

You and Your Submission

A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The degree of a vertex is the number of edges connected to this vertex.

You are given a weighted tree with nn vertices, each edge has a weight of 11. Let LL be the set of vertices with degree equal to 11.

You have to answer qq independent queries. In the ii-th query:

  1. You are given a positive integer xix_i.
  2. For all u,v∈Lu,v \in L such that u<vu \lt v, add edge (u,v)(u, v) with weight xix_i to the graph (initially the given tree).
  3. Find the diameter of the resulting graph.

The diameter of a graph is equal to max⁡1≤u<v≤nd⁡(u,v)\max\limits_{1 \le u \lt v \le n}{\operatorname{d}(u, v)}, where d⁡(u,v)\operatorname{d}(u, v) is the length of the shortest path between vertex uu and vertex vv.

— 我的任务是什么?
— 计算图的直径。

你与你的提交

树是一种无环的连通无向图。带权树为每条边赋予一个权重。顶点的度数是指与该顶点相连的边的数量。

给定一棵含 nn 个顶点的带权树,其中每条边的权重均为 11。令 LL 表示所有度数为 11 的顶点构成的集合。

你需要回答 qq 个相互独立的查询。在第 ii 个查询中:

  1. 给定一个正整数 xix_i;
  2. 对所有满足 u,v∈Lu,v \in L 且 u<vu \lt v 的顶点对,在图中(初始为给定的树)添加一条权重为 xix_i 的边 (u,v)(u, v);
  3. 求所得图的直径。

图的直径定义为 max⁡1≤u<v≤nd⁡(u,v)\max\limits_{1 \le u \lt v \le n}{\operatorname{d}(u, v)},其中 d⁡(u,v)\operatorname{d}(u, v) 表示顶点 uu 与顶点 vv 之间最短路径的长度。

输入格式

The first line contains a single integer nn (3≤n≤1063 \le n \le 10^6).

The second line contains n−1n - 1 integers p2,p3,…,pnp_2,p_3,\ldots,p_n (1≤pi<i1 \le p_i \lt i) indicating that there is an edge between vertices ii and pip_i. It is guaranteed that the given edges form a tree.

The third line contains a single integer qq (1≤q≤101 \le q \le 10).

The fourth line contains qq integers x1,x2,…,xqx_1,x_2,\ldots,x_q (1≤xi≤n1 \le x_i \le n). All xix_i are distinct.

第一行包含一个整数 nn(3≤n≤1063 \le n \le 10^6)。

第二行包含 n−1n - 1 个整数 p2,p3,…,pnp_2,p_3,\ldots,p_n(1≤pi<i1 \le p_i \lt i),表示顶点 ii 与顶点 pip_i 之间存在一条边。保证所给的边构成一棵树。

第三行包含一个整数 qq(1≤q≤101 \le q \le 10)。

第四行包含 qq 个整数 x1,x2,…,xqx_1,x_2,\ldots,x_q(1≤xi≤n1 \le x_i \le n)。所有 xix_i 互不相同。

输出格式

Print qq integers in a single line — the answers to the queries.

在一行中输出 qq 个整数——各查询的答案。

输入输出样例

  • 输入#1

    4
    1 2 2
    4
    1 2 3 4

    输出#1

    1 2 2 2
  • 输入#2

    7
    1 2 3 4 2 1
    7
    2 1 3 7 5 6 4

    输出#2

    3 3 4 5 5 5 4
  • 输入#3

    3
    1 2
    1
    1

    输出#3

    1

说明/提示

The graph in the first test after adding the edges:

添加边后第一个测试用例的图:

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

首页