-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path739B.cpp
More file actions
96 lines (88 loc) · 1.54 KB
/
Copy path739B.cpp
File metadata and controls
96 lines (88 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<bits/stdc++.h>
using namespace std;
typedef long long int64;
int n;
int64 a[200009];
struct Edge{
int to, nxt;
int64 w;
}e[400009];
int head[200009], tot;
int dep[200009], lca[200009][25];
int64 dis[200009];
int ans[200009];
void add(int x, int y, int w){
e[++tot].to = y;
e[tot].w = w;
e[tot].nxt = head[x];
head[x] = tot;
}
void dfs(int x, int fa){
dep[x] = dep[fa] + 1;
for(int i = head[x]; i; i = e[i].nxt){
int y = e[i].to;
if(y == fa) continue;
dis[y] = dis[x] + e[i].w;
lca[y][0] = x;
dfs(y, x);
}
}
void init_lca(){
for(int i = 1; i < 20; i++){
for(int j = 1; j <= n; j++){
lca[j][i] = lca[lca[j][i - 1]][i - 1];
}
}
}
int up(int x){
int cs = x;
for(int i = 19; i >= 0; i--){
if(dis[cs] - dis[lca[x][i]] <= a[cs]){
x = lca[x][i];
}
}
return lca[x][0];
}
void dfs2(int x, int fa){
for(int i = head[x]; i; i = e[i].nxt){
int y = e[i].to;
if(y == fa) continue;
int ffa = up(y);
ans[x]++;
ans[ffa]--;
dfs2(y, x);
}
}
void dfs3(int x, int fa){
for(int i = head[x]; i; i = e[i].nxt){
int y = e[i].to;
if(y == fa) continue;
dfs3(y, x);
ans[x] += ans[y];
}
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%lld", &a[i]);
}
for(int i = 1; i < n; i++){
int p, w;
scanf("%d%d", &p, &w);
add(p, i + 1, w);
add(i + 1, p, w);
}
// printf("79 Line\n");
dfs(1, 0);
// printf("81 Line\n");
init_lca();
// printf("83 Line\n");
dfs2(1, 0);
// printf("85 Line\n");
dfs3(1, 0);
// printf("87 Line\n");
for(int i = 1; i <= n; i++){
printf("%d ", ans[i]);
}
return 0;
}