题目描述:

Alyona and a tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ai. 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) ≤ au.

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 vcontrols u.

Input

The first line contains single integer n (1 ≤ n ≤ 2·105).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers written in the vertices.

The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n, 1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1).

It is guaranteed that the given graph is a tree.

Output

Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.

Examples

input

5

2 5 1 4 6

1 7

1 1

3 5

3 6

output

1 0 1 0 0

input

5

9 7 8 6 5

1 1

2 1

3 1

4 1

output

4 3 2 1 0

Note

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 1controls the vertex 5).

思路:

这道题是说,给一棵树,树上的每个节点都有一个a[i]值,每条边上都有权重。如果对一个节点来说,他到子树下面某个节点的距离(权值和)小于等于那个节点的a[i]值,那它就能控制这个节点。那么现在对于每个节点,看它能控制多少个节点。

如果我们先dfs一下这棵树,就可以求出来所有节点的深度(带权值)。然后怎么办?难道要看结点1,下面的结点有几个满足被控制条件,结点2,看下面有几个满足被控制的条件...?想想时间复杂度就很高,而且该用什么数据结构实现呢(我是蒟蒻我不知道)。

那转换一下关注对象,上面我们看的是对每个节点看他能控制那些节点,不如这样:对于每个节点我们看他能被那些节点控制。具体的,对节点u,如果p(p是u的祖先节点)满足\(depth[u]-depth[p]\leq a[u]\),那u就能被p控制。又发现这样一个事实,树上从上往下深度是递增的,意味着如果p满足了控制u的条件,那么p之下,u之上的节点都能控制u。因为到u的距离小于p到u的距离,从而小于a[u]。单增序列,我们只要找到遍历路径上第一个满足控制u的条件的节点就行了。这时,我们可以建一个\(vector\)来存储路径,路径是边,第一个属性是这条边到达点深度,第二个属性是到达点的标号。由于序列的单调性,使用lower_bound即可求出p节点来。

现在就是要把p节点下u节点上的节点的控制个数加一了,对于区间加法,我们可以考虑差分数组,在这里,也就是树上差分。具体的,将p节点父亲的控制数减一(如果找得到的话),将u节点的父结点控制数加u节点的控制数再加一。每个节点在\(dfs\)回溯时都做类似处理,到达p点就会得到正确的答案。

注意的是及时更新路径,包括加入新的边和弹出已返回的边(才知道原来\(vector\)有个pop_back()函数,真方便)。

代码:

#include <iostream>
#include <vector>
#define max_n 200005
using namespace std;
typedef pair<int,long long> PIL;
typedef pair<long long,int> PLI;
vector<PIL> edge[max_n];
int ans[max_n];
int n;
int a[max_n];
vector<PLI> path;
long long depth[max_n];
void dfs(int s)
{
/*cout << "s " << s << endl;
cout << "path" << path.size() << endl;
for(int i = 0;i<path.size();i++)
{
cout << path[i].first << " " << path[i].second << endl;
}*/
int node = lower_bound(path.begin(),path.end(),PLI(depth[s]-a[s],-1))-path.begin()-1;
//cout << "node " << node << endl;
if(node>=0) ans[path[node].second]--;
path.push_back(PLI(depth[s],s));
for(int i = 0;i<edge[s].size();i++)
{
int v = edge[s][i].first;
long long w = edge[s][i].second;
depth[v] = depth[s]+w;
dfs(v);
ans[s] += ans[v]+1;
}
path.pop_back();
}
int main()
{
cin >> n;
for(int i = 1;i<=n;i++)
{
cin >> a[i];
}
int v;
long long w;
for(int i = 1;i<n;i++)
{
cin >> v >> w;
edge[v].push_back(PIL(i+1,w));
}
dfs(1);
for(int i = 1;i<=n;i++)
{
cout << ans[i] << " ";
}
cout << endl;
return 0;
}

参考文章:

qscqesze,Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和,https://www.cnblogs.com/qscqesze/p/6103445.html(是大佬,不做过多解释)

键盘里的青春,Codeforces 739B Alyona and a tree (树上差分+二分),https://blog.csdn.net/qq_34374664/article/details/70246427(为什么又是他?别问,问就是带佬)

Codeforces E. Alyona and a tree(二分树上差分)的更多相关文章

  1. CodeForces 739B Alyona and a tree (二分+树上差分)

    <题目链接> 题目大意: 给定一颗带权树,树的根是1,树上每个点都有点权,并且还有边权.现在给出“控制”的定义:对一个点u,设v为其子树上的节点,且$dis(u,v)≤val[v]$,则称 ...

  2. 【CF739B】Alyona and a tree(树上差分,二分,树形DP)

    题意:给出一棵有根树,树上每个点.每条边都有一个权值. 现在给出“控制”的定义:对一个点u,设点v在其子树上,且dis(u,v)≤av,则称u控制v. 要求求出每个点控制了多少个点 n (1 ≤ n  ...

  3. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  4. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

  5. 洛谷 P2680 运输计划-二分+树上差分(边权覆盖)

    P2680 运输计划 题目背景 公元 20442044 年,人类进入了宇宙纪元. 题目描述 公元20442044 年,人类进入了宇宙纪元. L 国有 nn 个星球,还有 n-1n−1 条双向航道,每条 ...

  6. P2680 运输计划(二分+树上差分)

    P2680 运输计划 链接 分析: 二分+树上差分. 首先可以二分一个答案,那么所有比这个答案大的路径,都需要减去些东西才可以满足这个答案. 那么减去的这条边一定在所有的路径的交集上. 那么如果求快速 ...

  7. P2680 运输计划 二分+树上差分

    又咕咕了几天\(QwQ\) 思路:二分+树上差分 提交:\(\geq5\)次 错因:\(lca\)写错+卡了很久常数(哪位大佬帮我康康,有更好的写法请指出\(QwQ\)) 题解: 我们先将原问题转化为 ...

  8. XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)

    D. Alyona and a tree time limit per test  2 seconds memory limit per test  256 megabytes input  stan ...

  9. Codeforces 739B Alyona and a tree(树上路径倍增及差分)

    题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...

随机推荐

  1. LODOP表格水平居中3(宽度为百分比)

    如果一个表格在css样式等中设置了固定的宽度,想要实现表格在纸张中水平居中,可根据固定的宽度设置合适的左边距,如果打印项内容在打印项宽度中居中,可以设置打印项在纸张中居中.方法1:宽度固定,纸张大小固 ...

  2. PPT使用技巧

    推荐两个个人收藏的网站站点(非广告,好东西就应该share) 免费PPT模板下载网站 http://www.1ppt.com 免费图标下载网站 https://www.easyicon.net 一.P ...

  3. 前端框架之Bootstrap框架

    下载地址:https://v3.bootcss.com/,下载Bootstrap3版本 下载之后把文件中不需要的文件都删掉 需要获取的样式代码,可以直接从这些地方找到,然后复制 一.HTML页面导入文 ...

  4. golang在linux下的安装操作

  5. 上传docker镜像到阿里云镜像源

    阿里云docker镜像配置 阿里云用户名可以使用淘宝系的,或者新注册都行. a. 配置阿里云的镜像加速器:加速器 然后在线上创建`镜像仓库`,需要设置`命名空间`和`仓库名称`,然后接着操作下面的步骤 ...

  6. 在 Target 中获取项目引用的所有依赖(dll/NuGet/Project)的路径

    原文:在 Target 中获取项目引用的所有依赖(dll/NuGet/Project)的路径 在项目编译成 dll 之前,如何分析项目的所有依赖呢?可以在在项目的 Target 中去收集项目的依赖. ...

  7. java之spring之初始spring

    1.Spring 在多个框架中起到润滑剂的作用,桥梁的作用,纽带的作用. 2.Spring是一个容器,也是一个对象工厂.帮助程序员创建对象,管理对象. 3.Spring的体系结构: 4.学习sprin ...

  8. C# vb .net实现透明特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的透明效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...

  9. SQL Injection (Blind)

    Low级别基于布尔的盲注思路 1.判断是否存在注入,注入是字符型还是数字型 2.猜解当前数据库名 3.猜解数据库中的表名 4.猜解表中的字段名 5.猜解数据 判断是否有sql注入 输入1.1’ and ...

  10. ConnectionString属性(网速慢的情况下研究Connect Timeout)

    ConnectionString 类似于 OLE DB 连接字符串,但并不相同.与 OLE DB 或 ADO 不同,如果“Persist Security Info”值设置为 false(默认值),则 ...