传送门
题意
分析
求每个点控制了多少个点,等价于求每个点会被多少个点控制
考虑从根节点到每一个节点维护一条链,记录链上每一个节点到跟节点的距离,这样二分就可以求出距离当前点,最短的可以控制当前节点的点,然后对这个区间上每一个数+1即可,这个操作可以用差分来处理
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10,M = N * 2;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N],e[M],ne[M],w[M],idx;
int res[N];
int fa[N];
int a[N];
ll dep[N];
int id[N];
int n;
void add(int x,int y,int z){
ne[idx] = h[x],e[idx] = y,w[idx] = z,h[x] = idx++;
}
void dfs(int u,int f,int d,ll len){
fa[u] = f;
dep[d] = len;
id[d] = u;
int p = lower_bound(dep + 1,dep + 1 + d,len - a[u]) - dep;
res[fa[id[p]]]--,res[u]++;
for(int i = h[u];~i;i = ne[i]){
int j = e[i];
if(j == f) continue;
dfs(j,u,d + 1,len + w[i]);
}
}
void dfs(int u,int f){
for(int i = h[u];~i;i = ne[i]){
int j = e[i];
if(j == f) continue;
dfs(j,u);
res[u] += res[j];
}
}
int main() {
memset(h,-1,sizeof h);
read(n);
for(int i = 1;i <= n;i++) read(a[i]);
for(int i = 2;i <= n;i++){
int x,y;
read(x),read(y);
add(x,i,y),add(i,x,y);
}
dfs(1,0,1,0);
dfs(1,0);
for(int i = 1;i <= n;i++) printf("%d ",res[i] - 1);
return 0;
}