-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryLiftingWithDistance.cpp
More file actions
59 lines (54 loc) · 973 Bytes
/
binaryLiftingWithDistance.cpp
File metadata and controls
59 lines (54 loc) · 973 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int N;
int A[200001];
vector<int> adj[200001];
int P[18][200001];
long long D[18][200001];
int sum[200001];
void dfs(int u)
{
for(int i=1; i<18; i++)
{
P[i][u]=P[i-1][P[i-1][u]];
D[i][u]=D[i-1][u]+D[i-1][P[i-1][u]];
}
int x=A[u];
int p=u;
for(int i=17; i>=0; i--) if(D[i][p]<=x)
x-=D[i][p], p=P[i][p];
if(p!=1)
sum[P[0][p]]--;
sum[u]++;
for(auto& v: adj[u])
dfs(v);
}
void dfs2(int u)
{
for(auto& v: adj[u])
{
dfs2(v);
sum[u]+=sum[v];
}
}
int main()
{
scanf("%d", &N);
for(int i=1; i<=N; i++)
scanf("%d", A+i);
for(int i=2; i<=N; i++)
{
int a, b;
scanf("%d%d", &a, &b);
P[0][i]=a;
D[0][i]=b;
adj[a].push_back(i);
}
P[0][1]=1;
dfs(1);
dfs2(1);
for(int i=1; i<=N; i++)
printf("%d ", sum[i]-1);
printf("\n");
return 0;
}