Codeforces E. Alyona and a tree(二分树上差分)
题目描述:
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(二分树上差分)的更多相关文章
- CodeForces 739B Alyona and a tree (二分+树上差分)
<题目链接> 题目大意: 给定一颗带权树,树的根是1,树上每个点都有点权,并且还有边权.现在给出“控制”的定义:对一个点u,设v为其子树上的节点,且$dis(u,v)≤val[v]$,则称 ...
- 【CF739B】Alyona and a tree(树上差分,二分,树形DP)
题意:给出一棵有根树,树上每个点.每条边都有一个权值. 现在给出“控制”的定义:对一个点u,设点v在其子树上,且dis(u,v)≤av,则称u控制v. 要求求出每个点控制了多少个点 n (1 ≤ n ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- 洛谷 P2680 运输计划-二分+树上差分(边权覆盖)
P2680 运输计划 题目背景 公元 20442044 年,人类进入了宇宙纪元. 题目描述 公元20442044 年,人类进入了宇宙纪元. L 国有 nn 个星球,还有 n-1n−1 条双向航道,每条 ...
- P2680 运输计划(二分+树上差分)
P2680 运输计划 链接 分析: 二分+树上差分. 首先可以二分一个答案,那么所有比这个答案大的路径,都需要减去些东西才可以满足这个答案. 那么减去的这条边一定在所有的路径的交集上. 那么如果求快速 ...
- P2680 运输计划 二分+树上差分
又咕咕了几天\(QwQ\) 思路:二分+树上差分 提交:\(\geq5\)次 错因:\(lca\)写错+卡了很久常数(哪位大佬帮我康康,有更好的写法请指出\(QwQ\)) 题解: 我们先将原问题转化为 ...
- 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 ...
- Codeforces 739B Alyona and a tree(树上路径倍增及差分)
题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...
随机推荐
- ThinkPHP 中入口文件中的APP_DEBUG为TRUE时不报错,改为FALSE时报错
今天好不容易将一个新闻网做好了(ThinkPHP框架做的),但是,当我将入口文件中定义调试模式设为FALSE,即define('APP_DEBUG',False),然后再刷新网站的时候,就提示报错,报 ...
- Chaikin Curves in Processing
转自:https://sighack.com/post/chaikin-curves In this post, we’ll look at what Chaikin curves are, how ...
- SQL Server 中日志的的作用(Redo和Undo)
简介 之前我已经写了一个关于SQL Server日志的简单系列文章.本篇文章会进一步挖掘日志背后的一些概念,原理以及作用. 数据库的可靠性 在关系数据库系统中,我们需要数据库可靠,所谓的可靠就是当遇见 ...
- FastReport For Delphi7 通用安装方法
安装前请册除原有的FR控件. 1. "Tools|Environmet options..."中的"Library"标签面下"Library path ...
- 深度学习-Wasserstein GAN论文理解笔记
GAN存在问题 训练困难,G和D多次尝试没有稳定性,Loss无法知道能否优化,生成样本单一,改进方案靠暴力尝试 WGAN GAN的Loss函数选择不合适,使模型容易面临梯度消失,梯度不稳定,优化目标不 ...
- Oracle Round 函式 (四捨五入)
Oracle Round 函式 (四捨五入)描述 : 傳回一個數值,該數值是按照指定的小數位元數進行四捨五入運算的結果.SELECT ROUND( number, [ decimal_places ] ...
- SQL SERVER 查询所有表大小
DECLARE @T TABLE ( [name] VARCHAR(max), [rows] INT, reserved VARCHAR(max), data_size VARCHAR(max), i ...
- 基于hystrix的线程池隔离
hystrix进行资源隔离,其实是提供了一个抽象,叫做command,就是说,你如果要把对某一个依赖服务的所有调用请求,全部隔离在同一份资源池内 对这个依赖服务的所有调用请求,全部走这个资源池内的资源 ...
- Linux学习笔记之tail命令显示最后n行
tail :输出文件的最后几行. 用于linux查看日志的时候很方便,假如日志文件为:Console.log用法:1. tail Console.log tail # 输出文件最后10行的内容 2. ...
- oracle 数据库触发器,插入更新时间戳
1.首先建立一个测试表 CREATE TABLE TestTragger( UserId int Primary Key, Name VARCHAR() Not Null, CreateTime Ti ...