A10758 | Alyona and a tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Alyona has a tree with $n$ vertices. The root of the tree is the vertex $1$ . In each vertex Alyona wrote an positive integer, in the vertex $i$ she wrote $a_{i}$ . Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).
Let's define $dist(v,u)$ as the sum of the integers written on the edges of the simple path from $v$ to $u$ .
The vertex $v$ controls the vertex $u$ ( $v≠u$ ) if and only if $u$ is in the subtree of $v$ and $dist(v,u)<=a_{u}$ .
Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex $v$ what is the number of vertices $u$ such that $v$ controls $u$ .
Let's define $dist(v,u)$ as the sum of the integers written on the edges of the simple path from $v$ to $u$ .
The vertex $v$ controls the vertex $u$ ( $v≠u$ ) if and only if $u$ is in the subtree of $v$ and $dist(v,u)<=a_{u}$ .
Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex $v$ what is the number of vertices $u$ such that $v$ controls $u$ .
输入格式
The first line contains single integer $n$ ( $1<=n<=2·10^{5}$ ).
The second line contains $n$ integers $a_{1},a_{2},...,a_{n}$ ( $1<=a_{i}<=10^{9}$ ) — the integers written in the vertices.
The next $(n-1)$ lines contain two integers each. The $i$ -th of these lines contains integers $p_{i}$ and $w_{i}$ ( $1<=p_{i}<=n$ , $1<=w_{i}<=10^{9}$ ) — the parent of the $(i+1)$ -th vertex in the tree and the number written on the edge between $p_{i}$ and $(i+1)$ .
It is guaranteed that the given graph is a tree.
The second line contains $n$ integers $a_{1},a_{2},...,a_{n}$ ( $1<=a_{i}<=10^{9}$ ) — the integers written in the vertices.
The next $(n-1)$ lines contain two integers each. The $i$ -th of these lines contains integers $p_{i}$ and $w_{i}$ ( $1<=p_{i}<=n$ , $1<=w_{i}<=10^{9}$ ) — the parent of the $(i+1)$ -th vertex in the tree and the number written on the edge between $p_{i}$ and $(i+1)$ .
It is guaranteed that the given graph is a tree.
输出格式
Print $n$ integers — the $i$ -th of these numbers should be equal to the number of vertices that the $i$ -th vertex controls.
输入输出样例
输入 #1
5 2 5 1 4 6 1 7 1 1 3 5 3 6
输出 #1
1 0 1 0 0
输入 #2
5 9 7 8 6 5 1 1 2 1 3 1 4 1
输出 #2
4 3 2 1 0
In the example test case the vertex $1$ controls the vertex $3$ , the vertex $3$ controls the vertex $5$ (note that is doesn't mean the vertex $1$ controls the vertex $5$ ).
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted