您的位置:首页 > 理论基础 > 数据结构算法

Codeforces Round #381 (Div. 2) D. Alyona and a tree

2016-11-25 11:41 357 查看
D. Alyona and a tree

time limit per test
2 seconds

memory limit per test
256 megabytes

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).

思路:

1、dfs 求出每一个点距离树根的距离 dis
,用时间戳 in
,out
数组记录进出点的时间并将子节点的时间戳包含在父亲节点的时间戳之内;

2、对于每一个点,值为val
,处理出 node(1,i,dis[i]-val[i]) 与 node(2,i,dis[i]); 两组数据,并按第3个值升序排序;

3、再维护一个树状数组更新、求和即可。

#include <queue>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <assert.h>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a));
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define mod 1000000007
#define fre  freopen("input.txt","r",stdin)
#define fot  freopen("out.txt","r",stdout)
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
using namespace std;
#define N 200005
#define M 15
int n;
int in
,out
;
struct edge
{
int v,next,dis;
}e
;
int val
;
LL  dis
;
int head
;
int ans
;
struct node
{
int id,num;
LL length;
node(){}
node(int Id, int Num, LL Length){          //注意,这里是longlong  length
id = Id , num = Num , length = Length;
}
bool friend operator<(node a,node b){
if(a.length == b.length) return a.id < b.id;
return a.length < b.length;
}
}no[N<<1];
int cot;
void dfs(int x,LL len)
{
dis[x] = len;
++cot;
in[x] = cot;
for(int i=head[x]; ~i; i=e[i].next){
int v = e[i].v;
dfs(v, len+e[i].dis);
}
out[x]=cot;
}

int q
;                                       //树状数组
inline int getsum(int x)
{
int ans = 0;
while(x>0){
ans += q[x];
x -= x & -x;
}
return ans;
}
inline void add(int x)
{
while(x<=n){
q[x]++;
x += x & -x;
}
}
int main()
{
scan(n);
REP(i,1,n+1) scan(val[i]);
int cnt = 0;
mst(head,-1);
REPP(i,2,n){
int a,b;
scann(a,b);
e[cnt].v = i;
e[cnt].next = head[a];
e[cnt].dis  = b;
head[a] = cnt++;
}
dfs(1,0);
cnt = 0;
REP(i,1,n+1){
no[cnt++]=node(1,i,dis[i]-val[i]);
no[cnt++]=node(2,i,dis[i]);
}
sort(no,no+cnt);
REP(i,0,cnt){
if(no[i].id == 1){                              //  dis[i] - val[i]  用于对其他点进行更新,然而同时会使自己加一次,所以最后要 -1
// cout<<i<<" "<<no[i].num<<" "<<in[no[i].num]<<endl;
add(in[no[i].num]);
}else{
ans[no[i].num] = getsum(out[no[i].num]) - getsum(in[no[i].num]-1);
}
}
REP(i,1,n+1)  printf("%d ",ans[i] - 1);
}


c56b
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息