#include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<int,long long> PIL; typedef pair<long long,int> PLI; const int mod = 1e9+7; long long r[100005]; vector<PIL> T[100005]; bool used[100005]; int sz[100005]; long long dist[100005]; vector<vector<int>> G; vector<vector<int>> Gt; vector<int> vis; vector<int> ord; vector<int> numS; int cntS = 0; vector<vector<int>> GS; vector<int> ans; vector<int> val; void dfs_sz(int v, int p){ sz[v] = 1; for(PIL & u: T[v]) if(u.first != p){ dfs_sz(u.first, v); sz[v] += sz[u.first]; } } int find_centr(int v, int n){ for(PIL & u: T[v]){ if(!used[u.first] && sz[u.first] > n/2){ sz[v] -= sz[u.first], sz[u.first] = n; return find_centr(u.first, n); } } return v; } vector<PLI> rads; vector<PLI> dists; void dfs_dist(int v, int p, long long dist){ dists.push_back({dist, v}); if(dist <= r[v]){ rads.push_back({r[v]-dist, v}); } for(PIL & u: T[v]){ if(u.first != p && !used[u.first]) dfs_dist(u.first, v, dist+u.second); } } void add_edge(int a, int b){ //cout<<a<<" "<<b<<endl; if(G.size() <= a) G.resize(a+1); G[a].push_back(b); if(Gt.size() <= b) Gt.resize(b+1); Gt[b].push_back(a); } int num_v; void centr_decomp(int v){ int centr = find_centr(v, sz[v]); rads.clear(); dists.clear(); dfs_dist(centr, 0, 0); sort(rads.begin(), rads.end()); sort(dists.begin(), dists.end()); int ind = 0; num_v++; add_edge(num_v, centr); for(PLI & u: rads){ while(ind < dists.size() && dists[ind].first <= u.first){ add_edge(num_v, dists[ind].second); ind++; } add_edge(u.second, num_v); add_edge(num_v+1, num_v); num_v++; } used[centr] = true; for(PIL & u: T[centr]) if(!used[u.first]) centr_decomp(u.first); } void dfs(int v){ vis[v] = 1; for(int u: G[v]) if(vis[u] == 0) dfs(u); ord.push_back(v); } void dfs2(int v){ vis[v] = 2; numS[v] = cntS; for(int u: Gt[v]) if(vis[u] == 1) dfs2(u); } int ti = 4; int cnt = 0; void dfs3(int v){ vis[v] = ti; cnt += val[v]; for(int u: GS[v]) if(vis[u] != ti) dfs3(u); } int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; for(int i=1;i<=n;i++) cin>>r[i]; for(int i=1;i<n;i++){ int a,b; long long c; cin>>a>>b>>c; T[a].push_back({b,c}); T[b].push_back({a,c}); } num_v = n; dfs_sz(1, 0); centr_decomp(1); G.resize(num_v+2); Gt.resize(num_v+2); vis.resize(num_v+1); numS.resize(num_v+1); GS.resize(num_v+1); ans.resize(num_v+1); val.resize(num_v+1); for(int i=1;i<=num_v;i++) if(vis[i] == 0) dfs(i); reverse(ord.begin(), ord.end()); for(int v: ord) if(vis[v] == 1){ cntS++; dfs2(v); } for(int a=1;a<=num_v;a++){ for(int b: G[a]) if(numS[a] != numS[b]) GS[numS[a]].push_back(numS[b]); } for(int i=1;i<=n;i++) val[numS[i]]++; for(int i=1;i<=cntS;i++){ dfs3(i); ans[i] = cnt; cnt=0, ti++; } for(int i=1;i<=n;i++) cout<<ans[numS[i]]<<" "; cout<<endl; }
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<int,long long> PIL; typedef pair<long long,int> PLI; const int mod = 1e9+7; long long r[100005]; vector<PIL> T[100005]; bool used[100005]; int sz[100005]; long long dist[100005]; vector<vector<int>> G; vector<vector<int>> Gt; vector<int> vis; vector<int> ord; vector<int> numS; int cntS = 0; vector<vector<int>> GS; vector<int> ans; vector<int> val; void dfs_sz(int v, int p){ sz[v] = 1; for(PIL & u: T[v]) if(u.first != p){ dfs_sz(u.first, v); sz[v] += sz[u.first]; } } int find_centr(int v, int n){ for(PIL & u: T[v]){ if(!used[u.first] && sz[u.first] > n/2){ sz[v] -= sz[u.first], sz[u.first] = n; return find_centr(u.first, n); } } return v; } vector<PLI> rads; vector<PLI> dists; void dfs_dist(int v, int p, long long dist){ dists.push_back({dist, v}); if(dist <= r[v]){ rads.push_back({r[v]-dist, v}); } for(PIL & u: T[v]){ if(u.first != p && !used[u.first]) dfs_dist(u.first, v, dist+u.second); } } void add_edge(int a, int b){ //cout<<a<<" "<<b<<endl; if(G.size() <= a) G.resize(a+1); G[a].push_back(b); if(Gt.size() <= b) Gt.resize(b+1); Gt[b].push_back(a); } int num_v; void centr_decomp(int v){ int centr = find_centr(v, sz[v]); rads.clear(); dists.clear(); dfs_dist(centr, 0, 0); sort(rads.begin(), rads.end()); sort(dists.begin(), dists.end()); int ind = 0; num_v++; add_edge(num_v, centr); for(PLI & u: rads){ while(ind < dists.size() && dists[ind].first <= u.first){ add_edge(num_v, dists[ind].second); ind++; } add_edge(u.second, num_v); add_edge(num_v+1, num_v); num_v++; } used[centr] = true; for(PIL & u: T[centr]) if(!used[u.first]) centr_decomp(u.first); } void dfs(int v){ vis[v] = 1; for(int u: G[v]) if(vis[u] == 0) dfs(u); ord.push_back(v); } void dfs2(int v){ vis[v] = 2; numS[v] = cntS; for(int u: Gt[v]) if(vis[u] == 1) dfs2(u); } int ti = 4; int cnt = 0; void dfs3(int v){ vis[v] = ti; cnt += val[v]; for(int u: GS[v]) if(vis[u] != ti) dfs3(u); } int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; for(int i=1;i<=n;i++) cin>>r[i]; for(int i=1;i<n;i++){ int a,b; long long c; cin>>a>>b>>c; T[a].push_back({b,c}); T[b].push_back({a,c}); } num_v = n; dfs_sz(1, 0); centr_decomp(1); G.resize(num_v+2); Gt.resize(num_v+2); vis.resize(num_v+1); numS.resize(num_v+1); GS.resize(num_v+1); ans.resize(num_v+1); val.resize(num_v+1); for(int i=1;i<=num_v;i++) if(vis[i] == 0) dfs(i); reverse(ord.begin(), ord.end()); for(int v: ord) if(vis[v] == 1){ cntS++; dfs2(v); } for(int a=1;a<=num_v;a++){ for(int b: G[a]) if(numS[a] != numS[b]) GS[numS[a]].push_back(numS[b]); } for(int i=1;i<=n;i++) val[numS[i]]++; for(int i=1;i<=cntS;i++){ dfs3(i); ans[i] = cnt; cnt=0, ti++; } for(int i=1;i<=n;i++) cout<<ans[numS[i]]<<" "; cout<<endl; } |