#include <bits/stdc++.h> using namespace std; long long r[100005]; long long odl__[8006][8006]; int n; vector<pair<int, long long>> sc[100005]; class Data { public: long long range; int w, f; Data(long long _range, int _w, int _f) : range(_range), w(_w), f(_f) {} }; namespace LCA { const int S2 = 100005; int lca[S2][20]; int height[S2]; long long height2[S2]; void dfs(int w, int h, long long bh, int f = -1) { height[w] = h; height2[w] = bh; lca[w][0] = f; for (int i = 0; i < 19; i++) { if (lca[w][i] == -1 || lca[lca[w][i]][i] == -1) break; lca[w][i + 1] = lca[lca[w][i]][i]; } for (auto i : sc[w]) { if (i.first == f) continue; dfs(i.first, h + 1, bh + i.second, w); } } long long distance2(int a, int b) { if (height[b] > height[a]) swap(a, b); long long currentCost = height2[a] + height2[b]; for (int i = 19; i >= 0; i--) { if (height[a] - (1 << i) >= height[b]) { a = lca[a][i]; } } if (a == b) return currentCost - 2 * height2[a]; for (int i = 19; i >= 0; i--) { if (lca[a][i] == lca[b][i]) continue; a = lca[a][i]; b = lca[b][i]; } assert(lca[a][0] == lca[b][0]); // cout << lca[a][0] << '\n'; return currentCost - 2 * height2[lca[a][0]]; } } long long odl(int w, int f) { if (n <= 8000) { // cout << w << ' ' << f << ' ' << LCA::distance2(w, f) << ' ' << odl__[w][f] << '\n' // << flush; // assert(LCA::distance2(w, f) == odl__[w][f]); return odl__[w][f]; } return LCA::distance2(w, f); } void dfs(int w, int f, long long _odl, int s) { odl__[w][s] = _odl; for (auto i : sc[w]) { if (i.first == f) continue; dfs(i.first, w, _odl + i.second, s); } } int main() { ios_base::sync_with_stdio(0); srand(2137659); cin >> n; for (int i = 1; i <= n; i++) cin >> r[i]; for (int i = 1; i < n; i++) { long long a, b, c; cin >> a >> b >> c; sc[a].push_back({b, c}); sc[b].push_back({a, c}); } if (n <= 8000) { for (int i = 1; i <= n; i++) { dfs(i, 0, 0, i); } } LCA::dfs(1, 0, 0, 0); for (int i = 1; i <= n; i++) { int wn = 0; Data frs(r[i], i, 0); vector<Data> rec(1, frs), un; while (rec.size()) { wn++; swap(rec[rec.size() - 1], rec[rand() % rec.size()]); Data lst = rec.back(); // cout << lst.w << ' ' << lst.range << '\n'; rec.pop_back(); long long remR = max(lst.range, r[lst.w]); if (remR > lst.range) { for (auto &i : rec) i.range = max(i.range, remR - odl(lst.w, i.w)); for (int i = 0; i < un.size(); i++) { if (remR - odl(lst.w, un[i].w) > 0) { // cout << "REV " << un[i].w << ' ' << remR << ' ' << odl[lst.w][un[i].w] << ' ' << lst.w << '\n'; un[i].range = remR - odl(lst.w, un[i].w); swap(un[i], un[un.size() - 1]); rec.push_back(un.back()); un.pop_back(); i--; } } } for (auto j : sc[lst.w]) { if (j.first == lst.f) continue; Data nem(remR - j.second, j.first, lst.w); if (nem.range < 0) un.push_back(nem); else rec.push_back(nem); } } cout << wn << ' '; } }
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 | #include <bits/stdc++.h> using namespace std; long long r[100005]; long long odl__[8006][8006]; int n; vector<pair<int, long long>> sc[100005]; class Data { public: long long range; int w, f; Data(long long _range, int _w, int _f) : range(_range), w(_w), f(_f) {} }; namespace LCA { const int S2 = 100005; int lca[S2][20]; int height[S2]; long long height2[S2]; void dfs(int w, int h, long long bh, int f = -1) { height[w] = h; height2[w] = bh; lca[w][0] = f; for (int i = 0; i < 19; i++) { if (lca[w][i] == -1 || lca[lca[w][i]][i] == -1) break; lca[w][i + 1] = lca[lca[w][i]][i]; } for (auto i : sc[w]) { if (i.first == f) continue; dfs(i.first, h + 1, bh + i.second, w); } } long long distance2(int a, int b) { if (height[b] > height[a]) swap(a, b); long long currentCost = height2[a] + height2[b]; for (int i = 19; i >= 0; i--) { if (height[a] - (1 << i) >= height[b]) { a = lca[a][i]; } } if (a == b) return currentCost - 2 * height2[a]; for (int i = 19; i >= 0; i--) { if (lca[a][i] == lca[b][i]) continue; a = lca[a][i]; b = lca[b][i]; } assert(lca[a][0] == lca[b][0]); // cout << lca[a][0] << '\n'; return currentCost - 2 * height2[lca[a][0]]; } } long long odl(int w, int f) { if (n <= 8000) { // cout << w << ' ' << f << ' ' << LCA::distance2(w, f) << ' ' << odl__[w][f] << '\n' // << flush; // assert(LCA::distance2(w, f) == odl__[w][f]); return odl__[w][f]; } return LCA::distance2(w, f); } void dfs(int w, int f, long long _odl, int s) { odl__[w][s] = _odl; for (auto i : sc[w]) { if (i.first == f) continue; dfs(i.first, w, _odl + i.second, s); } } int main() { ios_base::sync_with_stdio(0); srand(2137659); cin >> n; for (int i = 1; i <= n; i++) cin >> r[i]; for (int i = 1; i < n; i++) { long long a, b, c; cin >> a >> b >> c; sc[a].push_back({b, c}); sc[b].push_back({a, c}); } if (n <= 8000) { for (int i = 1; i <= n; i++) { dfs(i, 0, 0, i); } } LCA::dfs(1, 0, 0, 0); for (int i = 1; i <= n; i++) { int wn = 0; Data frs(r[i], i, 0); vector<Data> rec(1, frs), un; while (rec.size()) { wn++; swap(rec[rec.size() - 1], rec[rand() % rec.size()]); Data lst = rec.back(); // cout << lst.w << ' ' << lst.range << '\n'; rec.pop_back(); long long remR = max(lst.range, r[lst.w]); if (remR > lst.range) { for (auto &i : rec) i.range = max(i.range, remR - odl(lst.w, i.w)); for (int i = 0; i < un.size(); i++) { if (remR - odl(lst.w, un[i].w) > 0) { // cout << "REV " << un[i].w << ' ' << remR << ' ' << odl[lst.w][un[i].w] << ' ' << lst.w << '\n'; un[i].range = remR - odl(lst.w, un[i].w); swap(un[i], un[un.size() - 1]); rec.push_back(un.back()); un.pop_back(); i--; } } } for (auto j : sc[lst.w]) { if (j.first == lst.f) continue; Data nem(remR - j.second, j.first, lst.w); if (nem.range < 0) un.push_back(nem); else rec.push_back(nem); } } cout << wn << ' '; } } |