739B Codeforces Alyona and a tree 树上差分+二分(倍增)
来源:互联网 发布:qq飞车noble数据 编辑:程序博客网 时间:2024/05/18 21:43
题目大意是给出一棵有根树,树上每个点、每条边都有一个权值。
现在给出“控制”的定义:对一个点u,设点v在其子树上,且
要求求出每个点控制了多少个点
模拟dfs过程,我们很容易发现dfs到点u时,其祖先节点到根的dis值都已经算出,且是单调递增,所以我们可以用二分或者倍增,在log的时间复杂度内找到深度最小的满足
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>#include <iostream>using namespace std;const int maxn = 200010;const int lg = 20;int n;long long a[maxn];long long c[maxn];struct edge{ int v,w,next;}e[maxn << 1];long long dep[maxn];int h[maxn],num = 0;int f[maxn][lg+1];int u,v;long long w;void build(int u,int v,int w){ num++; e[num].v = v; e[num].w = w; e[num].next = h[u]; h[u] = num;}void dfs1(int x,int fa){ int v; f[x][0] = fa; for(int i = 1; i < lg; i++) f[x][i] = f[f[x][i-1]][i-1]; int u = x; for(int i = lg - 1; i >= 0; i--) while(u > 1 && dep[x] - dep[f[u][i]] <= a[x]) u = f[u][i]; u = max(1,u); c[f[u][0]]--; c[f[x][0]]++; for(int i = h[x]; i; i = e[i].next) { v = e[i].v; if(v != fa) { dep[v] = dep[x] + e[i].w; dfs1(v,x); } }}void dfs2(int x,int f){ int v; for(int i = h[x]; i; i = e[i].next) { v = e[i].v; if(v != f) { dfs2(v,x); c[x] += c[v]; } }}int main(){ freopen("1.in","r",stdin); cin >> n; for(int i = 1; i <= n; i++) scanf("%I64d",&a[i]); for(int i = 2; i <= n; i++) { scanf("%d%I64d",&v,&w); build(i,v,w); build(v,i,w); } dfs1(1,0); dfs2(1,0); for(int i = 1; i <= n; i++) printf("%I64d%c",c[i],i == n ? '\n' : ' '); return 0;}
0 0
- 739B Codeforces Alyona and a tree 树上差分+二分(倍增)
- Codeforces 739B Alyona and a tree (树上差分+二分)
- Codeforces 740D Alyona and a tree 二分+树上差分
- Codeforces 739B【树上倍增+差分】
- Codeforces 739B(树上路径倍增及差分)
- codeforces 739b Alyona and a tree
- CODEFORCES 739B Alyona and a tree
- codeforces 739B B. Alyona and a tree
- Codeforces Round #381 (Div. 2) D Alyona and a tree(DFS树上搞事)
- CF 739B - Alyona and a tree
- 【NOIP2015】运输计划 {二分答案+倍增+树上差分}
- NOIP 2015 Day2 T3 运输计划(二分+dfs序+树上差分+倍增LCA)
- Codeforces739B. Alyona and a tree(树+二分)
- Codeforces Round #358 (Div. 2) -- C. Alyona and the Tree (树上的DFS)
- CodeForces 381 div.2 D. Alyona and a tree dfs序 二分 区间和
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树型前缀和+二分维护
- [Codeforces div1] Round 739C. Alyona and towers 线段树+差分数组
- noip运输计划(倍增lca,树上差分)
- linux安装mongodb
- android移动开发-单文件下载-基于官方DownLoadManager进行
- Windows系统中双击无法执行jar包的情况
- Xutils3.0框架的初始体验一
- save(),update(),saveOrUpdate()和merge()
- 739B Codeforces Alyona and a tree 树上差分+二分(倍增)
- 一行代码获取ViewPager中当前显示的Fragment
- LightOJ 1042 Secret Origins 贪心
- 欢迎使用CSDN-markdown编辑器
- 触摸屏与usb鼠标同时支持
- 百度地图的简单开发之引入基本地图以及修改地图样式(一)
- 队列的简单实现
- Android指纹识别
- 静态工厂方法VS构造器