#include <bits/stdc++.h> using namespace std; using ll = long long; using edge = pair<int, ll>; const int MAXN = 100010; const int INF = 123456789; vector<edge> G[MAXN]; ll radius[MAXN]; // data structure used to answer successor/predecessor queries struct DS { int n; int p2; vector<int> members; // position in list -> vertex id unordered_map<int, int> mapping; // vertex id -> position in list vector<ll> distances; vector<int> priorities; vector<pair<int, int>> segtree; }; void ds_update(DS *ds, int p, int x) { int nd = p + ds->p2; ds->segtree[nd] = { x, p + 1 }; nd /= 2; while (nd >= 1) { ds->segtree[nd] = min(ds->segtree[2 * nd], ds->segtree[2 * nd + 1]); nd /= 2; } } void ds_toggle_on(DS *ds, int v) { int p = ds->mapping[v]; ds_update(ds, p, ds->priorities[p]); } void ds_toggle_off(DS *ds, int v) { int p = ds->mapping[v]; ds_update(ds, p, INF); } int ds_query(DS *ds, ll dist) { int nd = 1; int l = 1; int r = ds->p2; pair<int, int> out = { INF, -1 }; while (l < r) { int mid = (l + r) / 2; if (mid <= ds->n && ds->distances[mid - 1] <= dist) { out = min(out, ds->segtree[2 * nd]); nd = 2 * nd + 1; l = mid + 1; } else { nd = 2 * nd; r = mid; } } if (l <= ds->n && ds->distances[l - 1] <= dist) { out = min(out, ds->segtree[nd]); } return out.first < INF ? out.second : -1; } /********************* CENTROID DECOMPOSITION *********************/ struct Blob { int root; vector<int> members; Blob *parent; vector<Blob*> children; ll dist_to_parent; DS *ds_successor; DS *ds_predecessor; }; bool locked[MAXN]; int subtree_size[MAXN]; // vertices -> blobs containing them Blob *blob_ptr[MAXN]; // find subtree sizes and store visited vertices void decompose_DFS(int v, int prevv, vector<int> &members) { subtree_size[v] = 1; members.push_back(v); for (auto [x, _] : G[v]) { if (x != prevv && !locked[x]) { decompose_DFS(x, v, members); subtree_size[v] += subtree_size[x]; } } } Blob *decompose(int v) { Blob *blob = new Blob(); blob->parent = nullptr; decompose_DFS(v, v, blob->members); if (blob->members.size() <= 1) { blob->root = blob->members.back(); blob_ptr[blob->members.back()] = blob; return blob; } int total = subtree_size[v]; for (auto x : blob->members) { int maxsubtree = total - subtree_size[x]; for (auto [y, _] : G[x]) { if (!locked[y] && subtree_size[y] < subtree_size[x]) { maxsubtree = max(maxsubtree, subtree_size[y]); } } if (maxsubtree * 2 <= total) { blob->root = x; blob_ptr[x] = blob; break; } } locked[blob->root] = true; for (auto [x, _] : G[blob->root]) { if (!locked[x]) { Blob *b = decompose(x); blob->children.push_back(b); b->parent = blob; } } return blob; } // for debug void print_blob(Blob *blob, int indent) { for (int i = 0; i < indent; i++) printf(" "); puts("-------"); for (int i = 0; i < indent; i++) printf(" "); printf("%d\n", blob->root); for (int i = 0; i < indent; i++) printf(" "); for (auto x : blob->members) printf("%d ", x); puts(""); for (int i = 0; i < indent; i++) printf(" "); for (auto x : blob->ds_successor->members) printf("%d ", x); puts(""); for (int i = 0; i < indent; i++) printf(" "); for (auto x : blob->ds_successor->distances) printf("%lld ", x); puts(""); for (int i = 0; i < indent; i++) printf(" "); for (auto x : blob->ds_successor->segtree) printf("(%d, %d) ", x.first, x.second); puts(""); for (auto b : blob->children) print_blob(b, indent + 1); } /************* DECOMP QUERIES *************/ bool mask[MAXN]; int priority[MAXN]; ll dist[MAXN]; void build_DSs_DFS(int v, int prevv, ll dst) { dist[v] = dst; for (auto [x, w] : G[v]) { if (x != prevv && !locked[x]) { build_DSs_DFS(x, v, dst + w); } } } void build_DSs(Blob *blob) { int n = blob->members.size(); int p2 = 1; while (p2 < n) p2 *= 2; build_DSs_DFS(blob->root, blob->root, 0); vector<pair<ll, int>> sorted_successor; vector<pair<ll, int>> sorted_predecessor; for (auto x : blob->members) { sorted_successor.push_back({ dist[x], x }); sorted_predecessor.push_back({ dist[x] - radius[x], x }); } sort(sorted_successor.begin(), sorted_successor.end()); sort(sorted_predecessor.begin(), sorted_predecessor.end()); // members, mapping, distances, priorities, segtree DS *ds_successor = new DS(); ds_successor->n = n; ds_successor->p2 = p2; ds_successor->members.resize(n); ds_successor->distances.resize(n); ds_successor->priorities.resize(n); ds_successor->segtree.resize(2 * p2 + 1); for (int i = 0; i < n; i++) { ds_successor->members[i] = sorted_successor[i].second; ds_successor->mapping[sorted_successor[i].second] = i; ds_successor->distances[i] = sorted_successor[i].first; ds_successor->priorities[i] = 0; } for (int i = 0; i <= 2 * p2; i++) { ds_successor->segtree[i] = { INF, -1 }; } DS *ds_predecessor = new DS(); ds_predecessor->n = n; ds_predecessor->p2 = p2; ds_predecessor->members.resize(n); ds_predecessor->distances.resize(n); ds_predecessor->priorities.resize(n); ds_predecessor->segtree.resize(2 * p2 + 1); for (int i = 0; i < n; i++) { ds_predecessor->members[i] = sorted_predecessor[i].second; ds_predecessor->mapping[sorted_predecessor[i].second] = i; ds_predecessor->distances[i] = sorted_predecessor[i].first; ds_predecessor->priorities[i] = 0; } for (int i = 0; i <= 2 * p2; i++) { ds_predecessor->segtree[i] = { INF, -1 }; } blob->ds_successor = ds_successor; blob->ds_predecessor = ds_predecessor; locked[blob->root] = true; for (auto b : blob->children) { b->dist_to_parent = dist[b->root]; build_DSs(b); } } void recalculate_DSs_priorities(Blob *blob) { int n = blob->ds_successor->n; int p2 = blob->ds_successor->p2; for (int i = 1; i <= n; i++) { blob->ds_successor ->priorities[i - 1] = priority[blob->ds_successor ->members[i - 1]]; blob->ds_predecessor->priorities[i - 1] = -priority[blob->ds_predecessor->members[i - 1]]; blob->ds_successor ->segtree[i + p2 - 1] = { priority[blob->ds_successor ->members[i - 1]], i }; blob->ds_predecessor->segtree[i + p2 - 1] = { -priority[blob->ds_predecessor->members[i - 1]], i }; } for (int i = p2 - 1; i >= 1; i--) { blob->ds_successor ->segtree[i] = min(blob->ds_successor ->segtree[2 * i], blob->ds_successor ->segtree[2 * i + 1]); blob->ds_predecessor->segtree[i] = min(blob->ds_predecessor->segtree[2 * i], blob->ds_predecessor->segtree[2 * i + 1]); } for (auto b : blob->children) { recalculate_DSs_priorities(b); } } void toggle_on(int v) { mask[v] = false; Blob *blob = blob_ptr[v]; while (blob != nullptr) { ds_toggle_on(blob->ds_successor, v); ds_toggle_on(blob->ds_predecessor, v); blob = blob->parent; } } void toggle_off(int v) { mask[v] = true; Blob *blob = blob_ptr[v]; while (blob != nullptr) { ds_toggle_off(blob->ds_successor, v); ds_toggle_off(blob->ds_predecessor, v); blob = blob->parent; } } int find_successor(int v) { Blob *blob = blob_ptr[v]; int r = -1; while (blob != nullptr) { ll rad = radius[v] - blob->ds_successor->distances[blob->ds_successor->mapping[v]]; int r2 = ds_query(blob->ds_successor, rad); if (r2 != -1) { r2 = blob->ds_successor->members[r2 - 1]; if (r == -1 || priority[r2] < priority[r]) { r = r2; } } blob = blob->parent; } return r; } int find_predecessor(int v) { Blob *blob = blob_ptr[v]; int r = -1; while (blob != nullptr) { ll rad = -blob->ds_successor->distances[blob->ds_successor->mapping[v]]; int r2 = ds_query(blob->ds_predecessor, rad); if (r2 != -1) { r2 = blob->ds_predecessor->members[r2 - 1]; if (r == -1 || priority[r2] > priority[r]) { r = r2; } } blob = blob->parent; } return r; } int ctr = 0; vector<vector<int>> SCCs; void DFS_SCCs_fwd(int v, vector<int> &stk) { ctr++; toggle_off(v); int r; while ((r = find_successor(v)) != -1) { DFS_SCCs_fwd(r, stk); } stk.push_back(v); } void DFS_SCCs_bwd(int v) { ctr++; toggle_off(v); SCCs.back().push_back(v); int r; while ((r = find_predecessor(v)) != -1) { DFS_SCCs_bwd(r); } } void find_SCCs(int n) { for (int i = 1; i <= n; i++) { toggle_on(i); } vector<int> stk; ctr = 0; for (int i = 1; i <= n; i++) { if (!mask[i]) { DFS_SCCs_fwd(i, stk); } } for (int i = 1; i <= n; i++) { toggle_on(i); } ctr = 0; while (stk.size() > 0) { int x = stk.back(); stk.pop_back(); if (!mask[x]) { SCCs.push_back({}); DFS_SCCs_bwd(x); } } } set<int> reachable[MAXN]; void find_important_edges(int n) { for (int i = 1; i <= n; i++) { toggle_on(i); } /*for (int i = 0; i < SCCs.size(); i++) { if (!mask[SCCs[i][0]]) { DFS_important(i); } }*/ for (int i = SCCs.size() - 1; i >= 0; i--) { for (auto x : SCCs[i]) { toggle_off(x); } reachable[i].insert(i); vector<int> visited; while (true) { int r = -1; for (auto x : SCCs[i]) { int r2 = find_successor(x); if (r2 != -1 && (r == -1 || priority[r2] < priority[r])) { r = r2; } } if (r != -1) { int j = priority[r]; visited.push_back(j); if (reachable[i].find(j) == reachable[i].end()) { for (auto k : reachable[j]) { reachable[i].insert(k); } } for (auto y : SCCs[j]) { toggle_off(y); } } else { break; } } for (auto x : SCCs[i]) { toggle_on(x); } for (auto j : visited) { for (auto y : SCCs[j]) { toggle_on(y); } } } } int DP[MAXN]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", radius + i); } for (int i = 1; i < n; i++) { int x, y; ll w; scanf("%d%d%lld", &x, &y, &w); G[x].push_back({ y, w }); G[y].push_back({ x, w }); } Blob *b = decompose(1); for (int i = 1; i <= n; i++) locked[i] = false; build_DSs(b); recalculate_DSs_priorities(b); find_SCCs(n); for (int i = 0; i < SCCs.size(); i++) { //printf("%d: ", i); for (auto x : SCCs[i]) { priority[x] = i; //printf("%d ", x); } //puts(""); } recalculate_DSs_priorities(b); find_important_edges(n); // O(n log^2 n) machine broke :c /*for (int i = SCCs.size() - 1; i >= 0; i--) { DP[i] += SCCs[i].size(); for (auto s : important_edges[i]) { DP[i] += DP[s]; //printf("%d -> %d\n", i, s); } }*/ // brucisz for (int i = 0; i < SCCs.size(); i++) { for (auto j : reachable[i]) { DP[i] += SCCs[j].size(); } } for (int i = 1; i <= n; i++) { printf("%d ", DP[priority[i]]); } puts(""); }
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | #include <bits/stdc++.h> using namespace std; using ll = long long; using edge = pair<int, ll>; const int MAXN = 100010; const int INF = 123456789; vector<edge> G[MAXN]; ll radius[MAXN]; // data structure used to answer successor/predecessor queries struct DS { int n; int p2; vector<int> members; // position in list -> vertex id unordered_map<int, int> mapping; // vertex id -> position in list vector<ll> distances; vector<int> priorities; vector<pair<int, int>> segtree; }; void ds_update(DS *ds, int p, int x) { int nd = p + ds->p2; ds->segtree[nd] = { x, p + 1 }; nd /= 2; while (nd >= 1) { ds->segtree[nd] = min(ds->segtree[2 * nd], ds->segtree[2 * nd + 1]); nd /= 2; } } void ds_toggle_on(DS *ds, int v) { int p = ds->mapping[v]; ds_update(ds, p, ds->priorities[p]); } void ds_toggle_off(DS *ds, int v) { int p = ds->mapping[v]; ds_update(ds, p, INF); } int ds_query(DS *ds, ll dist) { int nd = 1; int l = 1; int r = ds->p2; pair<int, int> out = { INF, -1 }; while (l < r) { int mid = (l + r) / 2; if (mid <= ds->n && ds->distances[mid - 1] <= dist) { out = min(out, ds->segtree[2 * nd]); nd = 2 * nd + 1; l = mid + 1; } else { nd = 2 * nd; r = mid; } } if (l <= ds->n && ds->distances[l - 1] <= dist) { out = min(out, ds->segtree[nd]); } return out.first < INF ? out.second : -1; } /********************* CENTROID DECOMPOSITION *********************/ struct Blob { int root; vector<int> members; Blob *parent; vector<Blob*> children; ll dist_to_parent; DS *ds_successor; DS *ds_predecessor; }; bool locked[MAXN]; int subtree_size[MAXN]; // vertices -> blobs containing them Blob *blob_ptr[MAXN]; // find subtree sizes and store visited vertices void decompose_DFS(int v, int prevv, vector<int> &members) { subtree_size[v] = 1; members.push_back(v); for (auto [x, _] : G[v]) { if (x != prevv && !locked[x]) { decompose_DFS(x, v, members); subtree_size[v] += subtree_size[x]; } } } Blob *decompose(int v) { Blob *blob = new Blob(); blob->parent = nullptr; decompose_DFS(v, v, blob->members); if (blob->members.size() <= 1) { blob->root = blob->members.back(); blob_ptr[blob->members.back()] = blob; return blob; } int total = subtree_size[v]; for (auto x : blob->members) { int maxsubtree = total - subtree_size[x]; for (auto [y, _] : G[x]) { if (!locked[y] && subtree_size[y] < subtree_size[x]) { maxsubtree = max(maxsubtree, subtree_size[y]); } } if (maxsubtree * 2 <= total) { blob->root = x; blob_ptr[x] = blob; break; } } locked[blob->root] = true; for (auto [x, _] : G[blob->root]) { if (!locked[x]) { Blob *b = decompose(x); blob->children.push_back(b); b->parent = blob; } } return blob; } // for debug void print_blob(Blob *blob, int indent) { for (int i = 0; i < indent; i++) printf(" "); puts("-------"); for (int i = 0; i < indent; i++) printf(" "); printf("%d\n", blob->root); for (int i = 0; i < indent; i++) printf(" "); for (auto x : blob->members) printf("%d ", x); puts(""); for (int i = 0; i < indent; i++) printf(" "); for (auto x : blob->ds_successor->members) printf("%d ", x); puts(""); for (int i = 0; i < indent; i++) printf(" "); for (auto x : blob->ds_successor->distances) printf("%lld ", x); puts(""); for (int i = 0; i < indent; i++) printf(" "); for (auto x : blob->ds_successor->segtree) printf("(%d, %d) ", x.first, x.second); puts(""); for (auto b : blob->children) print_blob(b, indent + 1); } /************* DECOMP QUERIES *************/ bool mask[MAXN]; int priority[MAXN]; ll dist[MAXN]; void build_DSs_DFS(int v, int prevv, ll dst) { dist[v] = dst; for (auto [x, w] : G[v]) { if (x != prevv && !locked[x]) { build_DSs_DFS(x, v, dst + w); } } } void build_DSs(Blob *blob) { int n = blob->members.size(); int p2 = 1; while (p2 < n) p2 *= 2; build_DSs_DFS(blob->root, blob->root, 0); vector<pair<ll, int>> sorted_successor; vector<pair<ll, int>> sorted_predecessor; for (auto x : blob->members) { sorted_successor.push_back({ dist[x], x }); sorted_predecessor.push_back({ dist[x] - radius[x], x }); } sort(sorted_successor.begin(), sorted_successor.end()); sort(sorted_predecessor.begin(), sorted_predecessor.end()); // members, mapping, distances, priorities, segtree DS *ds_successor = new DS(); ds_successor->n = n; ds_successor->p2 = p2; ds_successor->members.resize(n); ds_successor->distances.resize(n); ds_successor->priorities.resize(n); ds_successor->segtree.resize(2 * p2 + 1); for (int i = 0; i < n; i++) { ds_successor->members[i] = sorted_successor[i].second; ds_successor->mapping[sorted_successor[i].second] = i; ds_successor->distances[i] = sorted_successor[i].first; ds_successor->priorities[i] = 0; } for (int i = 0; i <= 2 * p2; i++) { ds_successor->segtree[i] = { INF, -1 }; } DS *ds_predecessor = new DS(); ds_predecessor->n = n; ds_predecessor->p2 = p2; ds_predecessor->members.resize(n); ds_predecessor->distances.resize(n); ds_predecessor->priorities.resize(n); ds_predecessor->segtree.resize(2 * p2 + 1); for (int i = 0; i < n; i++) { ds_predecessor->members[i] = sorted_predecessor[i].second; ds_predecessor->mapping[sorted_predecessor[i].second] = i; ds_predecessor->distances[i] = sorted_predecessor[i].first; ds_predecessor->priorities[i] = 0; } for (int i = 0; i <= 2 * p2; i++) { ds_predecessor->segtree[i] = { INF, -1 }; } blob->ds_successor = ds_successor; blob->ds_predecessor = ds_predecessor; locked[blob->root] = true; for (auto b : blob->children) { b->dist_to_parent = dist[b->root]; build_DSs(b); } } void recalculate_DSs_priorities(Blob *blob) { int n = blob->ds_successor->n; int p2 = blob->ds_successor->p2; for (int i = 1; i <= n; i++) { blob->ds_successor ->priorities[i - 1] = priority[blob->ds_successor ->members[i - 1]]; blob->ds_predecessor->priorities[i - 1] = -priority[blob->ds_predecessor->members[i - 1]]; blob->ds_successor ->segtree[i + p2 - 1] = { priority[blob->ds_successor ->members[i - 1]], i }; blob->ds_predecessor->segtree[i + p2 - 1] = { -priority[blob->ds_predecessor->members[i - 1]], i }; } for (int i = p2 - 1; i >= 1; i--) { blob->ds_successor ->segtree[i] = min(blob->ds_successor ->segtree[2 * i], blob->ds_successor ->segtree[2 * i + 1]); blob->ds_predecessor->segtree[i] = min(blob->ds_predecessor->segtree[2 * i], blob->ds_predecessor->segtree[2 * i + 1]); } for (auto b : blob->children) { recalculate_DSs_priorities(b); } } void toggle_on(int v) { mask[v] = false; Blob *blob = blob_ptr[v]; while (blob != nullptr) { ds_toggle_on(blob->ds_successor, v); ds_toggle_on(blob->ds_predecessor, v); blob = blob->parent; } } void toggle_off(int v) { mask[v] = true; Blob *blob = blob_ptr[v]; while (blob != nullptr) { ds_toggle_off(blob->ds_successor, v); ds_toggle_off(blob->ds_predecessor, v); blob = blob->parent; } } int find_successor(int v) { Blob *blob = blob_ptr[v]; int r = -1; while (blob != nullptr) { ll rad = radius[v] - blob->ds_successor->distances[blob->ds_successor->mapping[v]]; int r2 = ds_query(blob->ds_successor, rad); if (r2 != -1) { r2 = blob->ds_successor->members[r2 - 1]; if (r == -1 || priority[r2] < priority[r]) { r = r2; } } blob = blob->parent; } return r; } int find_predecessor(int v) { Blob *blob = blob_ptr[v]; int r = -1; while (blob != nullptr) { ll rad = -blob->ds_successor->distances[blob->ds_successor->mapping[v]]; int r2 = ds_query(blob->ds_predecessor, rad); if (r2 != -1) { r2 = blob->ds_predecessor->members[r2 - 1]; if (r == -1 || priority[r2] > priority[r]) { r = r2; } } blob = blob->parent; } return r; } int ctr = 0; vector<vector<int>> SCCs; void DFS_SCCs_fwd(int v, vector<int> &stk) { ctr++; toggle_off(v); int r; while ((r = find_successor(v)) != -1) { DFS_SCCs_fwd(r, stk); } stk.push_back(v); } void DFS_SCCs_bwd(int v) { ctr++; toggle_off(v); SCCs.back().push_back(v); int r; while ((r = find_predecessor(v)) != -1) { DFS_SCCs_bwd(r); } } void find_SCCs(int n) { for (int i = 1; i <= n; i++) { toggle_on(i); } vector<int> stk; ctr = 0; for (int i = 1; i <= n; i++) { if (!mask[i]) { DFS_SCCs_fwd(i, stk); } } for (int i = 1; i <= n; i++) { toggle_on(i); } ctr = 0; while (stk.size() > 0) { int x = stk.back(); stk.pop_back(); if (!mask[x]) { SCCs.push_back({}); DFS_SCCs_bwd(x); } } } set<int> reachable[MAXN]; void find_important_edges(int n) { for (int i = 1; i <= n; i++) { toggle_on(i); } /*for (int i = 0; i < SCCs.size(); i++) { if (!mask[SCCs[i][0]]) { DFS_important(i); } }*/ for (int i = SCCs.size() - 1; i >= 0; i--) { for (auto x : SCCs[i]) { toggle_off(x); } reachable[i].insert(i); vector<int> visited; while (true) { int r = -1; for (auto x : SCCs[i]) { int r2 = find_successor(x); if (r2 != -1 && (r == -1 || priority[r2] < priority[r])) { r = r2; } } if (r != -1) { int j = priority[r]; visited.push_back(j); if (reachable[i].find(j) == reachable[i].end()) { for (auto k : reachable[j]) { reachable[i].insert(k); } } for (auto y : SCCs[j]) { toggle_off(y); } } else { break; } } for (auto x : SCCs[i]) { toggle_on(x); } for (auto j : visited) { for (auto y : SCCs[j]) { toggle_on(y); } } } } int DP[MAXN]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", radius + i); } for (int i = 1; i < n; i++) { int x, y; ll w; scanf("%d%d%lld", &x, &y, &w); G[x].push_back({ y, w }); G[y].push_back({ x, w }); } Blob *b = decompose(1); for (int i = 1; i <= n; i++) locked[i] = false; build_DSs(b); recalculate_DSs_priorities(b); find_SCCs(n); for (int i = 0; i < SCCs.size(); i++) { //printf("%d: ", i); for (auto x : SCCs[i]) { priority[x] = i; //printf("%d ", x); } //puts(""); } recalculate_DSs_priorities(b); find_important_edges(n); // O(n log^2 n) machine broke :c /*for (int i = SCCs.size() - 1; i >= 0; i--) { DP[i] += SCCs[i].size(); for (auto s : important_edges[i]) { DP[i] += DP[s]; //printf("%d -> %d\n", i, s); } }*/ // brucisz for (int i = 0; i < SCCs.size(); i++) { for (auto j : reachable[i]) { DP[i] += SCCs[j].size(); } } for (int i = 1; i <= n; i++) { printf("%d ", DP[priority[i]]); } puts(""); } |