CF1712F.Triameter

普及/提高-

通过率:0%

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,vLu,v \in L such that u<vu < 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 max1u<vnd(u,v)\max\limits_{1 \le u < 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 .

输入格式

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

The second line contains n1n - 1 integers p2,p3,,pnp_2,p_3,\ldots,p_n ( 1pi<i1 \le p_i < 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 ( 1q101 \le q \le 10 ).

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

输出格式

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

输入输出样例

  • 输入#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:

首页