#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5+7; vector<pair<int,ll> > G[N]; vector<int> sm[N]; ll R[N]; void dfs(int v,int par,ll can,int un){ if (v!=par){ sm[un].push_back(v); } if (v!=par && R[v]>=can){ return; } for(auto [to,w]:G[v]){ if (to==par || w>can){ continue; } dfs(to,v,can-w,un); } } int cnt = 0; bool vis[N]; void clk(int v){ if (vis[v]){ return; } cnt += 1; vis[v] = 1; for(int to:sm[v]){ clk(to); } } void cl(int v){ if (!vis[v]){ return; } cnt -= 1; vis[v] = 0; for(int to:sm[v]){ cl(to); } } void solve(){ int n; cin>>n; for(int i = 1;i<=n;i+=1){ cin>>R[i]; } for(int i = 1;i<n;i+=1){ int u,v; ll w; cin>>u>>v>>w; G[u].push_back({v,w}); G[v].push_back({u,w}); } for(int i = 1;i<=n;i+=1){ dfs(i,i,R[i],i); } for(int i = 1;i<=n;i+=1){ clk(i); cout<<cnt<<' '; cl(i); } cout<<endl; } signed main(){ int tt = 1; while(tt--){ solve(); } }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5+7; vector<pair<int,ll> > G[N]; vector<int> sm[N]; ll R[N]; void dfs(int v,int par,ll can,int un){ if (v!=par){ sm[un].push_back(v); } if (v!=par && R[v]>=can){ return; } for(auto [to,w]:G[v]){ if (to==par || w>can){ continue; } dfs(to,v,can-w,un); } } int cnt = 0; bool vis[N]; void clk(int v){ if (vis[v]){ return; } cnt += 1; vis[v] = 1; for(int to:sm[v]){ clk(to); } } void cl(int v){ if (!vis[v]){ return; } cnt -= 1; vis[v] = 0; for(int to:sm[v]){ cl(to); } } void solve(){ int n; cin>>n; for(int i = 1;i<=n;i+=1){ cin>>R[i]; } for(int i = 1;i<n;i+=1){ int u,v; ll w; cin>>u>>v>>w; G[u].push_back({v,w}); G[v].push_back({u,w}); } for(int i = 1;i<=n;i+=1){ dfs(i,i,R[i],i); } for(int i = 1;i<=n;i+=1){ clk(i); cout<<cnt<<' '; cl(i); } cout<<endl; } signed main(){ int tt = 1; while(tt--){ solve(); } } |