Codeforces Round 864 (Div. 2)
F
Li Hua and Path
Codeforces
3000
原题
← E→
翻译中…

Li Hua has a tree of $n$ vertices and $n-1$ edges. The vertices are numbered from $1$ to $n$.

翻译中…

A pair of vertices $(u,v)$ ($u \lt v$) is considered cute if exactly one of the following two statements is true:

翻译中…
  • $u$ is the vertex with the minimum index among all vertices on the path $(u,v)$.
  • $v$ is the vertex with the maximum index among all vertices on the path $(u,v)$.
翻译中…

There will be $m$ operations. In each operation, he decides an integer $k_j$, then inserts a vertex numbered $n+j$ to the tree, connecting with the vertex numbered $k_j$.

翻译中…

He wants to calculate the number of cute pairs before operations and after each operation.

翻译中…

Suppose you were Li Hua, please solve this problem.

翻译中…

Input

翻译中…

The first line contains the single integer $n$ ($2\le n\le 2\cdot 10^5$) — the number of vertices in the tree.

翻译中…

Next $n-1$ lines contain the edges of the tree. The $i$-th line contains two integers $u_i$ and $v_i$ ($1\le u_i,v_i\le n$; $u_i\ne v_i$) — the corresponding edge. The given edges form a tree.

翻译中…

The next line contains the single integer $m$ ($1\le m\le 2\cdot 10^5$) — the number of operations.

翻译中…

Next $m$ lines contain operations — one operation per line. The $j$-th operation contains one integer $k_j$ ($1\le k_j \lt n+j$) — a vertex.

翻译中…

Output

翻译中…

Print $m+1$ integers — the number of cute pairs before operations and after each operation.

翻译中…

Note

翻译中…

The initial tree is shown in the following picture:

翻译中…

image

翻译中…

There are $11$ cute pairs — $(1,5),(2,3),(2,4),(2,6),(2,7),(3,4),(3,6),(3,7),(4,5),(5,7),(6,7)$.

翻译中…

Similarly, we can count the cute pairs after each operation and the result is $15$ and $19$.

翻译中…

样例

Input #1
7
2 1
1 3
1 4
4 6
4 7
6 5
2
5
6
Output #1
11
15
19
InputCopy OutputCopy
题目信息
题号
1797F
时间限制
2 s
内存限制
256 MB
标签
数据结构
DFS 及类似
分治
并查集
树

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