#pragma GCC optimize("unroll-loops") #pragma GCC target("mmx,sse,sse2,sse3,ssse3") #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) int n; vector<vector<pair<int, ll>>> G; vector<ll> radius; vector<vi> reach; namespace centroid { vector<bool> on; vi size, comp; vector<ll> dist; void dfs(int v, int p, ll d) { comp.pb(v); dist[v] = d; each(e, G[v]) { if (e.x != p && on[e.x]) { dfs(e.x, v, d + e.y); } } } void process(int v) { comp.clear(); dfs(v, -1, 0); vector<pair<ll, int>> seq; each(u, comp) { if (dist[u] <= radius[u]) { seq.pb({radius[u]-dist[u], u}); } seq.pb({dist[u], -u-1}); } int tail = -1; ll tailW = -1; bool hasIn = 1; sort(all(seq)); each(p, seq) { if (p.y < 0) { if (hasIn && p.x != tailW) { int b = sz(reach); reach.emplace_back(); if (tail != -1) reach[b].pb(tail); tail = b; tailW = p.x; hasIn = 0; } reach[tail].pb(-p.y-1); } else if (tail != -1) { reach[p.y].pb(tail); hasIn = 1; } } if (!hasIn && tail == sz(reach)-1) { reach.pop_back(); } } void computeSize(int v, int p) { size[v] = 1; each(e, G[v]) if (e.x != p && on[e.x]) { computeSize(e.x, v); size[v] += size[e.x]; } } void decomp(int v) { computeSize(v, -1); int p = -1, s = size[v]; loop: each(e, G[v]) { if (e.x != p && on[e.x] && size[e.x] > s/2) { p = v; v = e.x; goto loop; } } process(v); on[v] = 0; each(e, G[v]) if (on[e.x]) decomp(e.x); } void run() { on.resize(sz(G), 1); size.resize(sz(G)); dist.resize(sz(G)); reach.resize(sz(G)); decomp(0); } }; struct SCC : vi { vector<vi> comps; vi S; SCC() {} SCC(vector<vi>& H) : vi(sz(H),-1), S(sz(H)) { rep(i, 0, sz(H)) if (!S[i]) dfs(H, i); } int dfs(vector<vi>& H, int v) { int low = S[v] = sz(S); S.pb(v); each(e, H[v]) if (at(e) < 0) low = min(low, S[e] ?: dfs(H, e)); if (low == S[v]) { comps.pb({}); rep(i, S[v], sz(S)) { at(S[i]) = sz(comps)-1; comps.back().pb(S[i]); } S.resize(S[v]); } return low; } }; constexpr int BLOCK = 512; // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36041 int popcount64(uint64_t x) { x = (x & 0x5555555555555555ULL) + ((x >> 1) & 0x5555555555555555ULL); x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL); x = (x & 0x0F0F0F0F0F0F0F0FULL) + ((x >> 4) & 0x0F0F0F0F0F0F0F0FULL); return int((x * 0x0101010101010101ULL) >> 56); } int popcount(const bitset<BLOCK>& mask) { constexpr size_t LEN = sizeof(bitset<BLOCK>) / sizeof(uint64_t); uint64_t *raw = (uint64_t*)&mask; int x = 0; for (size_t i = 0; i < LEN; i++) { x += popcount64(raw[i]); } return x; } void load() { cin >> n; G.resize(n); radius.resize(n); each(r, radius) { cin >> r; } rep(i, 1, n) { int a, b; ll c; cin >> a >> b >> c; a--; b--; G[a].pb({b, c}); G[b].pb({a, c}); } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); load(); centroid::run(); SCC scc(reach); vector<vi> dag(sz(scc.comps)); vector<bool> has(sz(dag)); rep(i, 0, sz(dag)) { each(v, scc.comps[i]) { each(e, reach[v]) { int j = scc[e]; if (i != j) dag[i].pb(j); } if (v < n) { has[i] = 1; } } } each(v, dag) { sort(all(v)); v.erase(unique(all(v)), v.end()); } vi processOrd; rep(i, 0, n) processOrd.pb(scc[i]); sort(all(processOrd)); vector<bitset<BLOCK>> mask(sz(dag)); vector<bool> any(sz(dag)); vi ans(sz(dag)); for (int i = 0; i < n; i += BLOCK) { each(vec, mask) vec.reset(); any.assign(sz(dag), 0); rep(j, i, min(i+BLOCK, n)) { int v = processOrd[j]; mask[v].set(j-i); any[v] = 1; } rep(j, processOrd[i], sz(dag)) { each(e, dag[j]) if (any[e]) { mask[j] |= mask[e]; any[j] = 1; } if (any[j] && has[j]) { ans[j] += popcount(mask[j]); } } } rep(i, 0, n) cout << ans[scc[i]] << ' '; cout << '\n'; cout << flush; _Exit(0); }
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | #pragma GCC optimize("unroll-loops") #pragma GCC target("mmx,sse,sse2,sse3,ssse3") #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) int n; vector<vector<pair<int, ll>>> G; vector<ll> radius; vector<vi> reach; namespace centroid { vector<bool> on; vi size, comp; vector<ll> dist; void dfs(int v, int p, ll d) { comp.pb(v); dist[v] = d; each(e, G[v]) { if (e.x != p && on[e.x]) { dfs(e.x, v, d + e.y); } } } void process(int v) { comp.clear(); dfs(v, -1, 0); vector<pair<ll, int>> seq; each(u, comp) { if (dist[u] <= radius[u]) { seq.pb({radius[u]-dist[u], u}); } seq.pb({dist[u], -u-1}); } int tail = -1; ll tailW = -1; bool hasIn = 1; sort(all(seq)); each(p, seq) { if (p.y < 0) { if (hasIn && p.x != tailW) { int b = sz(reach); reach.emplace_back(); if (tail != -1) reach[b].pb(tail); tail = b; tailW = p.x; hasIn = 0; } reach[tail].pb(-p.y-1); } else if (tail != -1) { reach[p.y].pb(tail); hasIn = 1; } } if (!hasIn && tail == sz(reach)-1) { reach.pop_back(); } } void computeSize(int v, int p) { size[v] = 1; each(e, G[v]) if (e.x != p && on[e.x]) { computeSize(e.x, v); size[v] += size[e.x]; } } void decomp(int v) { computeSize(v, -1); int p = -1, s = size[v]; loop: each(e, G[v]) { if (e.x != p && on[e.x] && size[e.x] > s/2) { p = v; v = e.x; goto loop; } } process(v); on[v] = 0; each(e, G[v]) if (on[e.x]) decomp(e.x); } void run() { on.resize(sz(G), 1); size.resize(sz(G)); dist.resize(sz(G)); reach.resize(sz(G)); decomp(0); } }; struct SCC : vi { vector<vi> comps; vi S; SCC() {} SCC(vector<vi>& H) : vi(sz(H),-1), S(sz(H)) { rep(i, 0, sz(H)) if (!S[i]) dfs(H, i); } int dfs(vector<vi>& H, int v) { int low = S[v] = sz(S); S.pb(v); each(e, H[v]) if (at(e) < 0) low = min(low, S[e] ?: dfs(H, e)); if (low == S[v]) { comps.pb({}); rep(i, S[v], sz(S)) { at(S[i]) = sz(comps)-1; comps.back().pb(S[i]); } S.resize(S[v]); } return low; } }; constexpr int BLOCK = 512; // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36041 int popcount64(uint64_t x) { x = (x & 0x5555555555555555ULL) + ((x >> 1) & 0x5555555555555555ULL); x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL); x = (x & 0x0F0F0F0F0F0F0F0FULL) + ((x >> 4) & 0x0F0F0F0F0F0F0F0FULL); return int((x * 0x0101010101010101ULL) >> 56); } int popcount(const bitset<BLOCK>& mask) { constexpr size_t LEN = sizeof(bitset<BLOCK>) / sizeof(uint64_t); uint64_t *raw = (uint64_t*)&mask; int x = 0; for (size_t i = 0; i < LEN; i++) { x += popcount64(raw[i]); } return x; } void load() { cin >> n; G.resize(n); radius.resize(n); each(r, radius) { cin >> r; } rep(i, 1, n) { int a, b; ll c; cin >> a >> b >> c; a--; b--; G[a].pb({b, c}); G[b].pb({a, c}); } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); load(); centroid::run(); SCC scc(reach); vector<vi> dag(sz(scc.comps)); vector<bool> has(sz(dag)); rep(i, 0, sz(dag)) { each(v, scc.comps[i]) { each(e, reach[v]) { int j = scc[e]; if (i != j) dag[i].pb(j); } if (v < n) { has[i] = 1; } } } each(v, dag) { sort(all(v)); v.erase(unique(all(v)), v.end()); } vi processOrd; rep(i, 0, n) processOrd.pb(scc[i]); sort(all(processOrd)); vector<bitset<BLOCK>> mask(sz(dag)); vector<bool> any(sz(dag)); vi ans(sz(dag)); for (int i = 0; i < n; i += BLOCK) { each(vec, mask) vec.reset(); any.assign(sz(dag), 0); rep(j, i, min(i+BLOCK, n)) { int v = processOrd[j]; mask[v].set(j-i); any[v] = 1; } rep(j, processOrd[i], sz(dag)) { each(e, dag[j]) if (any[e]) { mask[j] |= mask[e]; any[j] = 1; } if (any[j] && has[j]) { ans[j] += popcount(mask[j]); } } } rep(i, 0, n) cout << ans[scc[i]] << ' '; cout << '\n'; cout << flush; _Exit(0); } |