#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector ran(n, 0ll); REP (i, n) cin >> ran[i]; vector tree(n, vector<pair<int, LL>>()); REP (i, n - 1) { int u, v; LL w; cin >> u >> v >> w; --u; --v; tree[u].emplace_back(v, w); tree[v].emplace_back(u, w); } int licz = 0; vector odw(n, 0); vector graph(n, vector<int>()); vector rgraph(n, vector<int>()); function<void(int, int, int, LL)> dfs = [&](int v, int pop, int skad, LL dis) { if (ran[v] >= dis && v != skad) { graph[v].emplace_back(skad); rgraph[skad].emplace_back(v); return; } for (auto [u, w] : tree[v]) { if (u == pop) continue; dfs(u, v, skad, dis + w); } }; REP (i, n) dfs(i, -1, i, 0); stack<int> stc; function<void(int)> dfs1 = [&](int v) { odw[v] = licz; for (int u : graph[v]) if (odw[u] < licz) dfs1(u); stc.emplace(v); }; ++licz; REP (i, n) if (odw[i] < licz) dfs1(i); int num = 0; vector sss(n, 0), sz(n + 1, 0); function<void(int)> dfs2 = [&](int v) { ++sz[sss[v]]; for (int u : rgraph[v]) if (!sss[u]) { sss[u] = sss[v]; dfs2(u); } }; while (ssize(stc)) { int v = stc.top(); stc.pop(); if (sss[v]) continue; ++num; sss[v] = num; dfs2(v); } debug(sss); vector ngraph(num + 1, set<int>()); vector deg(num + 1, 0); REP (i, n) for (int u : graph[i]) if (sss[i] != sss[u]) { ngraph[sss[i]].emplace(sss[u]); } int ans; vector odp(num + 1, 0); vector owd(num + 1, 0); function<void(int)> wyn = [&](int v) { owd[v] = licz; ans += sz[v]; for (int u : ngraph[v]) if (owd[u] < licz) wyn(u); }; FOR (i, 1, num) { ++licz; ans = 0; wyn(i); odp[i] = ans; } REP (i, n) { if (i) cout << ' '; cout << odp[sss[i]]; } cout << '\n'; }
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 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector ran(n, 0ll); REP (i, n) cin >> ran[i]; vector tree(n, vector<pair<int, LL>>()); REP (i, n - 1) { int u, v; LL w; cin >> u >> v >> w; --u; --v; tree[u].emplace_back(v, w); tree[v].emplace_back(u, w); } int licz = 0; vector odw(n, 0); vector graph(n, vector<int>()); vector rgraph(n, vector<int>()); function<void(int, int, int, LL)> dfs = [&](int v, int pop, int skad, LL dis) { if (ran[v] >= dis && v != skad) { graph[v].emplace_back(skad); rgraph[skad].emplace_back(v); return; } for (auto [u, w] : tree[v]) { if (u == pop) continue; dfs(u, v, skad, dis + w); } }; REP (i, n) dfs(i, -1, i, 0); stack<int> stc; function<void(int)> dfs1 = [&](int v) { odw[v] = licz; for (int u : graph[v]) if (odw[u] < licz) dfs1(u); stc.emplace(v); }; ++licz; REP (i, n) if (odw[i] < licz) dfs1(i); int num = 0; vector sss(n, 0), sz(n + 1, 0); function<void(int)> dfs2 = [&](int v) { ++sz[sss[v]]; for (int u : rgraph[v]) if (!sss[u]) { sss[u] = sss[v]; dfs2(u); } }; while (ssize(stc)) { int v = stc.top(); stc.pop(); if (sss[v]) continue; ++num; sss[v] = num; dfs2(v); } debug(sss); vector ngraph(num + 1, set<int>()); vector deg(num + 1, 0); REP (i, n) for (int u : graph[i]) if (sss[i] != sss[u]) { ngraph[sss[i]].emplace(sss[u]); } int ans; vector odp(num + 1, 0); vector owd(num + 1, 0); function<void(int)> wyn = [&](int v) { owd[v] = licz; ans += sz[v]; for (int u : ngraph[v]) if (owd[u] < licz) wyn(u); }; FOR (i, 1, num) { ++licz; ans = 0; wyn(i); odp[i] = ans; } REP (i, n) { if (i) cout << ' '; cout << odp[sss[i]]; } cout << '\n'; } |