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}$ ...
 
随机推荐
- Putty——支持Telnet,ssh连接的连接软件
			
简介 PuTTY 的官方网站:http://www.chiark.greenend.org.uk/~sgtatham/putty/,截止到 2007年6月,发布的最高稳定版本是 0.6.PuTTY 是 ...
 - spring boot实现切割分片上传
			
文件上传是web开发中经常会遇到的 springboot的默认配置为10MB,大于10M的是传不上服务器的,需要修改默认配置 但是如果修改支持大文件又会增加服务器的负担. 当文件大于一定程度时,不仅服 ...
 - aspnetcore identity  result.Succeeded  SignInManager.IsSignedIn(User) false?
			
登陆返回的是 result.Succeeded 为什么跳转到其他页面SignInManager.IsSignedIn(User)为false呢? result.Succeeded _signInMan ...
 - 去除Chrome“请停用以开发者模式运行的扩展程序”提示
			
将version.dll放在chrome同级目录,重启浏览器( 79.0.3945.79版本后已失效)
 - Spring Security的RBAC数据模型嵌入
			
1.简介  基于角色的权限访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中,权限与角色相关联,用户通过成 ...
 - ELK学习笔记之logstash的各个场景应用(配置文件均已实践过)
			
详细请参考该文 https://www.cnblogs.com/qingqing74647464/p/9378385.html
 - ChineseNumber 转换
			
中文数字转换 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p> * &l ...
 - C# 练习题 判断1至输入数值之间有多少个素数,并输出所有素数。
			
题目:判断1至输入数值之间有多少个素数,并输出所有素数.1.程序分析:判断素数的方法:用一个数分别去除2到当前数-1,如果能被整除,则表明此数不是素数,反之是素数. class Program { / ...
 - MEF在WCF REST中实际应用2(Global.asax注册)
			
IOCContainer文件: public class IOCContainer { /// <summary> /// 容器 /// </summary> public s ...
 - java之aop
			
1.AOP:aspect orientied programming 面向切面编程.就是横向编程. 2.面向切面编程是在不改变原有代码的情况下增加新的功能. 3.在spring中面向切面编程有两种应用 ...