【30.36%】【codeforces 740D】Alyona and a tree
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 v controls 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 1 controls the vertex 5).
【题目链接】:http://codeforces.com/contest/740/problem/D
【题解】
可以用一个类似”前缀长度”的东西来搞;
设dis[x]表示根节点到x节点上的边权和.
则x这个节点是被u节点控制的,当且仅当
dis[x]-dis[u]<=a[x];
即dis[x]-a[x] <= dis[u];
而dis这个数组如果在dfs序上,肯定是单调递增的.
所以维护一下dfs序、维护一下dis数组;
在处理x的出度的时候
找出dis数组中第一个满足dis[x]-a[x]<=dis[u]
的u节点;
则u->x的路径上的所有点都能够控制x节点.
而u节点以上的节点都不能控制x节点.
设u节点的dfs序的上一个节点为y
则让ans[y]–;
然后在dfs的时候累加答案即可;
设当前节点为x,出度节点为y
则ans[x]+=ans[y];
当遇到那些ans被减过的节点(即执行过ans[x]–的节点x);
则在算的时候就会把那个不属于它的节点给扣掉.
而以上的节点相应的也会受“ans[x]–”的也不会算那些不属于它们的节点了.
一开始让ans[x]都等于1;
最后再减去1即可.
这个方法很巧妙。
如果不理解就多想想吧.
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define all(x) x.begin(),x.end()
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 2e5+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n;
LL a[MAXN];
LL ans[MAXN],dis[MAXN];
vector <LL> w[MAXN];
vector <int> G[MAXN];
vector < pair<LL,int> > temp;
void dfs(int x,int fa)
{
ans[x] = 1;
LL t = dis[x]-a[x];
int pos = lower_bound(all(temp),mp(t,0))-temp.begin();
pos--;
if (pos >= 0)
ans[temp[pos].se]--;
temp.pb(mp(dis[x],x));
int len = G[x].size();
rep1(i,0,len-1)
{
int y = G[x][i];
if (y==fa) continue;
dis[y] = dis[x] + w[x][i];
dfs(y,x);
ans[x] += ans[y];
}
temp.pop_back();
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rel(a[i]);
rep1(i,2,n)
{
int fa;LL cost;
rei(fa);rel(cost);
G[fa].pb(i);
w[fa].pb(cost);
G[i].pb(fa);
w[i].pb(cost);
}
dfs(1,-1);
rep1(i,1,n)
{
printf("%I64d",ans[i]-1);
if (i==n)
puts("");
else
putchar(' ');
}
return 0;
}
【30.36%】【codeforces 740D】Alyona and a tree的更多相关文章
- codeforces 381 D Alyona and a tree(倍增)(前缀数组)
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 【Round #36 (Div. 2 only) C】Socks Pairs
[题目链接]:https://csacademy.com/contest/round-36/task/socks-pairs/ [题意] 给你n种颜色的袜子,每种颜色颜色的袜子有ai只; 假设你在取袜 ...
- 【Round #36 (Div. 2 only) B】Safe Spots
[题目链接]:https://csacademy.com/contest/round-36/task/safe-spots/ [题意] 给你n个数字构成的序列; 每个位置上的数都由0和1组成; 对于每 ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- codeforces 682C C. Alyona and the Tree(dfs)
题目链接: C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input ...
- 【30.93%】【codeforces 558E】A Simple Task
time limit per test5 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【36.86%】【codeforces 558B】Amr and The Large Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【30.49%】【codeforces 569A】Music
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【30.23%】【codeforces 552C】Vanya and Scales
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- UVALive 5292 Critical Links
Critical Links Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- Oracle11gR2 Windows 7 64bit and PL/SQL Developer排错
1.Environment variable: "PATH" - This test checks whether the length of the environment va ...
- 设计模式(7)-结构型模式-Bridge模式
2.结构性模式 2.2 BRIDGE模式 别名:handle/body 这个模式体现了组合相对于继承的优势. 2.2.1动机 当一个抽象可能有多个实现时,通经常使用继承来协调它们.抽象类定义对该抽象 ...
- android抓取各种log的方法
1.logcat (四类log buffer是main,radio.system.events) adb wait-for-device logcat adb logcat -v time > ...
- weblogic虚拟路径配置
首发地址 https://blog.leapmie.com/archives/344/ 前言 weblogic的虚拟路径配置有两种: 一种是在项目下配置,即在weblogic.xml中配置,该方法配置 ...
- 洛谷——P1443 马的遍历
https://www.luogu.org/problem/show?pid=1443#sub 题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达 ...
- Xcode6:No architectures to compile for(ONLY_ACTIVE_ARCH=YES...)
1.问题描写叙述 Xcode6真机測试旧project,不能执行,报错例如以下: 2.解决方式 问题非常明显,当前的执行模式设置了ONLY_ACTIVE_ARCH为Yes,当时的project的Val ...
- SQL查询练习二(From LeetCode)
请选用MySQL进行测试. 1.将男性和女性的工资互换(E) 思路:使用case when进行条件判断,在使用update进行修改 update salary set sex = case sex w ...
- JQuery 各节点获取函数:父节点,子节点,兄弟节点
jQuery.parent(expr) //找父元素 jQuery.parents(expr) //找到所有祖先元素,不限于父元素 jQuery.children ...
- 【Codeforces Round #431 (Div. 2) A】Odds and Ends
[链接]点击打开链接 [题意] 让你把一个数组分成奇数个部分. 且每个部分的长度都是奇数. [题解] 很简单的脑洞题. 开头和结尾一定要为奇数,然后 n为奇数的话,就选整个数组咯. n为偶数的话,不能 ...