Codeforces Round 813 (Div. 2)
F
Triameter
Codeforces
3200
原题
← E2→
翻译中…
  • — 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 $n$ vertices, each edge has a weight of $1$. Let $L$ be the set of vertices with degree equal to $1$.

翻译中…

You have to answer $q$ independent queries. In the $i$-th query:

翻译中…
  1. You are given a positive integer $x_i$.
  2. For all $u,v \in L$ such that $u \lt v$, add edge $(u, v)$ with weight $x_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\limits_{1 \le u \lt v \le n}{\operatorname{d}(u, v)}$, where $\operatorname{d}(u, v)$ is the length of the shortest path between vertex $u$ and vertex $v$.

翻译中…

Input

翻译中…

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

翻译中…

The second line contains $n - 1$ integers $p_2,p_3,\ldots,p_n$ ($1 \le p_i \lt i$) indicating that there is an edge between vertices $i$ and $p_i$. It is guaranteed that the given edges form a tree.

翻译中…

The third line contains a single integer $q$ ($1 \le q \le 10$).

翻译中…

The fourth line contains $q$ integers $x_1,x_2,\ldots,x_q$ ($1 \le x_i \le n$). All $x_i$ are distinct.

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

The graph in the first test after adding the edges:

翻译中…

image

翻译中…

样例

Input #1
4
1 2 2
4
1 2 3 4
Output #1
1 2 2 2
InputCopy OutputCopy
Input #2
7
1 2 3 4 2 1
7
2 1 3 7 5 6 4
Output #2
3 3 4 5 5 5 4
InputCopy OutputCopy
Input #3
3
1 2
1
1
Output #3
1
InputCopy OutputCopy
题目信息
题号
1712F
时间限制
4.5 s
内存限制
768 MB
标签
二分
数据结构
DFS 及类似
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。