A15865 | Li Hua and Path
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 < 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.
A pair of vertices $(u,v)$ ( $u < 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.
输入格式
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 < n+j$ ) — a vertex.
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 < n+j$ ) — a vertex.
输出格式
Print $m+1$ integers — the number of cute pairs before operations and after each operation.
输入输出样例
输入 #1
7 2 1 1 3 1 4 4 6 4 7 6 5 2 5 6
输出 #1
11 15 19
The initial tree is shown in the following picture:
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted