#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } namespace atcoder { namespace internal { template <class E> struct csr { std::vector<int> start; std::vector<E> elist; explicit csr(int n, const std::vector<std::pair<int, E>>& edges) : start(n + 1), elist(edges.size()) { for (auto e : edges) { start[e.first + 1]++; } for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } auto counter = start; for (auto e : edges) { elist[counter[e.first]++] = e.second; } } }; // Reference: // R. Tarjan, // Depth-First Search and Linear Graph Algorithms struct scc_graph { public: explicit scc_graph(int n) : _n(n) {} int num_vertices() { return _n; } void add_edge(int from, int to) { edges.push_back({from, {to}}); } // @return pair of (# of scc, scc id) std::pair<int, std::vector<int>> scc_ids() { auto g = csr<edge>(_n, edges); int now_ord = 0, group_num = 0; std::vector<int> visited, low(_n), ord(_n, -1), ids(_n); visited.reserve(_n); auto dfs = [&](auto self, int v) -> void { low[v] = ord[v] = now_ord++; visited.push_back(v); for (int i = g.start[v]; i < g.start[v + 1]; i++) { auto to = g.elist[i].to; if (ord[to] == -1) { self(self, to); low[v] = std::min(low[v], low[to]); } else { low[v] = std::min(low[v], ord[to]); } } if (low[v] == ord[v]) { while (true) { int u = visited.back(); visited.pop_back(); ord[u] = _n; ids[u] = group_num; if (u == v) break; } group_num++; } }; for (int i = 0; i < _n; i++) { if (ord[i] == -1) dfs(dfs, i); } for (auto& x : ids) { x = group_num - 1 - x; } return {group_num, ids}; } std::vector<std::vector<int>> scc() { auto ids = scc_ids(); int group_num = ids.first; std::vector<int> counts(group_num); for (auto x : ids.second) counts[x]++; std::vector<std::vector<int>> groups(ids.first); for (int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for (int i = 0; i < _n; i++) { groups[ids.second[i]].push_back(i); } return groups; } int _n; struct edge { int to; }; std::vector<std::pair<int, edge>> edges; }; } // namespace internal struct scc_graph { public: scc_graph() : internal(0) {} explicit scc_graph(int n) : internal(n) {} void add_edge(int from, int to) { int n = internal.num_vertices(); assert(0 <= from && from < n); assert(0 <= to && to < n); internal.add_edge(from, to); } std::vector<std::vector<int>> scc() { return internal.scc(); } internal::scc_graph internal; }; } // namespace atcoder atcoder::scc_graph sccG; using edges_w = vector<pair<int, ll>>; vector<edges_w> G; void dfs_init(int i, int p, ll r, int fr) { for (auto [n, c] : G[i]) { if (n == p || c > r) continue; sccG.add_edge(fr, n); if (c != r) dfs_init(n, i, r - c, fr); } } template <size_t BSZ> vi find_pre(vvi &acyc, vi& multip) { int n = acyc.size(); assert(n <= (int)BSZ && (int)multip.size() == n); vi ans(n); vector<bitset<BSZ>> dp(n); repr(i, n) { dp[i][i] = true; for (int j : acyc[i]) dp[i] |= dp[j]; for (int j = i; j < n; ++j) { if (dp[i][j]) ans[i] += multip[j]; } } return ans; } void solve() { int n; cin >> n; vector<ll> r(n); for (ll &l : r) cin >> l; sccG = atcoder::scc_graph(n); G.resize(n); debug(); rep(_, n-1) { int a, b; ll c; cin >> a >> b >> c; --a; --b; G[a].emplace_back(b, c); G[b].emplace_back(a, c); } debug(); rep(i, n) { dfs_init(i, -1, r[i], i); } debug(); auto sccs = sccG.scc(); debug(sccs); int sccs_sz = (int)sccs.size(); vi where(n); vi multip(sccs_sz); debug(); rep(id, sccs_sz) { multip[id] = sccs[id].size(); for (int j : sccs[id]) where[j] = id; } debug(); vector<vector<bool>> seen(sccs_sz, vector<bool>(sccs_sz)); vvi acycG(sccs_sz); for (auto [a, bb] : sccG.internal.edges) { a = where[a]; int b = where[bb.to]; if (seen[a][b]) continue; seen[a][b] = true; acycG[a].push_back(b); } debug(acycG, where, multip); vi ans; if (sccs_sz < 20) ans = find_pre<20>(acycG, multip); else if (sccs_sz < 255) ans = find_pre<255>(acycG, multip); else if (sccs_sz < 505) ans = find_pre<505>(acycG, multip); else if (sccs_sz < 755) ans = find_pre<755>(acycG, multip); else if (sccs_sz < 1005) ans = find_pre<1005>(acycG, multip); else if (sccs_sz < 1225) ans = find_pre<1225>(acycG, multip); else if (sccs_sz < 1505) ans = find_pre<1505>(acycG, multip); else if (sccs_sz < 2005) ans = find_pre<2005>(acycG, multip); else if (sccs_sz < 2505) ans = find_pre<2505>(acycG, multip); else if (sccs_sz < 3005) ans = find_pre<3005>(acycG, multip); else if (sccs_sz < 3505) ans = find_pre<3505>(acycG, multip); else if (sccs_sz < 4005) ans = find_pre<4005>(acycG, multip); else if (sccs_sz < 4505) ans = find_pre<4505>(acycG, multip); else if (sccs_sz < 5005) ans = find_pre<5005>(acycG, multip); else if (sccs_sz < 5505) ans = find_pre<5505>(acycG, multip); else if (sccs_sz < 6005) ans = find_pre<6005>(acycG, multip); else if (sccs_sz < 7005) ans = find_pre<7005>(acycG, multip); else if (sccs_sz < 8005) ans = find_pre<8005>(acycG, multip); else if (sccs_sz < 9005) ans = find_pre<9005>(acycG, multip); else if (sccs_sz < 10005) ans = find_pre<10005>(acycG, multip); else if (sccs_sz < 12005) ans = find_pre<12005>(acycG, multip); else if (sccs_sz < 14005) ans = find_pre<14005>(acycG, multip); else if (sccs_sz < 16005) ans = find_pre<16005>(acycG, multip); else if (sccs_sz < 18005) ans = find_pre<18005>(acycG, multip); else if (sccs_sz < 20005) ans = find_pre<20005>(acycG, multip); else if (sccs_sz < 25005) ans = find_pre<25005>(acycG, multip); else if (sccs_sz < 30005) ans = find_pre<30005>(acycG, multip); else if (sccs_sz < 50005) ans = find_pre<50005>(acycG, multip); else ans = find_pre<100005>(acycG, multip); rep(i, n) cout << ans[where[i]] << ' '; cout << '\n'; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); return 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 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } namespace atcoder { namespace internal { template <class E> struct csr { std::vector<int> start; std::vector<E> elist; explicit csr(int n, const std::vector<std::pair<int, E>>& edges) : start(n + 1), elist(edges.size()) { for (auto e : edges) { start[e.first + 1]++; } for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } auto counter = start; for (auto e : edges) { elist[counter[e.first]++] = e.second; } } }; // Reference: // R. Tarjan, // Depth-First Search and Linear Graph Algorithms struct scc_graph { public: explicit scc_graph(int n) : _n(n) {} int num_vertices() { return _n; } void add_edge(int from, int to) { edges.push_back({from, {to}}); } // @return pair of (# of scc, scc id) std::pair<int, std::vector<int>> scc_ids() { auto g = csr<edge>(_n, edges); int now_ord = 0, group_num = 0; std::vector<int> visited, low(_n), ord(_n, -1), ids(_n); visited.reserve(_n); auto dfs = [&](auto self, int v) -> void { low[v] = ord[v] = now_ord++; visited.push_back(v); for (int i = g.start[v]; i < g.start[v + 1]; i++) { auto to = g.elist[i].to; if (ord[to] == -1) { self(self, to); low[v] = std::min(low[v], low[to]); } else { low[v] = std::min(low[v], ord[to]); } } if (low[v] == ord[v]) { while (true) { int u = visited.back(); visited.pop_back(); ord[u] = _n; ids[u] = group_num; if (u == v) break; } group_num++; } }; for (int i = 0; i < _n; i++) { if (ord[i] == -1) dfs(dfs, i); } for (auto& x : ids) { x = group_num - 1 - x; } return {group_num, ids}; } std::vector<std::vector<int>> scc() { auto ids = scc_ids(); int group_num = ids.first; std::vector<int> counts(group_num); for (auto x : ids.second) counts[x]++; std::vector<std::vector<int>> groups(ids.first); for (int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for (int i = 0; i < _n; i++) { groups[ids.second[i]].push_back(i); } return groups; } int _n; struct edge { int to; }; std::vector<std::pair<int, edge>> edges; }; } // namespace internal struct scc_graph { public: scc_graph() : internal(0) {} explicit scc_graph(int n) : internal(n) {} void add_edge(int from, int to) { int n = internal.num_vertices(); assert(0 <= from && from < n); assert(0 <= to && to < n); internal.add_edge(from, to); } std::vector<std::vector<int>> scc() { return internal.scc(); } internal::scc_graph internal; }; } // namespace atcoder atcoder::scc_graph sccG; using edges_w = vector<pair<int, ll>>; vector<edges_w> G; void dfs_init(int i, int p, ll r, int fr) { for (auto [n, c] : G[i]) { if (n == p || c > r) continue; sccG.add_edge(fr, n); if (c != r) dfs_init(n, i, r - c, fr); } } template <size_t BSZ> vi find_pre(vvi &acyc, vi& multip) { int n = acyc.size(); assert(n <= (int)BSZ && (int)multip.size() == n); vi ans(n); vector<bitset<BSZ>> dp(n); repr(i, n) { dp[i][i] = true; for (int j : acyc[i]) dp[i] |= dp[j]; for (int j = i; j < n; ++j) { if (dp[i][j]) ans[i] += multip[j]; } } return ans; } void solve() { int n; cin >> n; vector<ll> r(n); for (ll &l : r) cin >> l; sccG = atcoder::scc_graph(n); G.resize(n); debug(); rep(_, n-1) { int a, b; ll c; cin >> a >> b >> c; --a; --b; G[a].emplace_back(b, c); G[b].emplace_back(a, c); } debug(); rep(i, n) { dfs_init(i, -1, r[i], i); } debug(); auto sccs = sccG.scc(); debug(sccs); int sccs_sz = (int)sccs.size(); vi where(n); vi multip(sccs_sz); debug(); rep(id, sccs_sz) { multip[id] = sccs[id].size(); for (int j : sccs[id]) where[j] = id; } debug(); vector<vector<bool>> seen(sccs_sz, vector<bool>(sccs_sz)); vvi acycG(sccs_sz); for (auto [a, bb] : sccG.internal.edges) { a = where[a]; int b = where[bb.to]; if (seen[a][b]) continue; seen[a][b] = true; acycG[a].push_back(b); } debug(acycG, where, multip); vi ans; if (sccs_sz < 20) ans = find_pre<20>(acycG, multip); else if (sccs_sz < 255) ans = find_pre<255>(acycG, multip); else if (sccs_sz < 505) ans = find_pre<505>(acycG, multip); else if (sccs_sz < 755) ans = find_pre<755>(acycG, multip); else if (sccs_sz < 1005) ans = find_pre<1005>(acycG, multip); else if (sccs_sz < 1225) ans = find_pre<1225>(acycG, multip); else if (sccs_sz < 1505) ans = find_pre<1505>(acycG, multip); else if (sccs_sz < 2005) ans = find_pre<2005>(acycG, multip); else if (sccs_sz < 2505) ans = find_pre<2505>(acycG, multip); else if (sccs_sz < 3005) ans = find_pre<3005>(acycG, multip); else if (sccs_sz < 3505) ans = find_pre<3505>(acycG, multip); else if (sccs_sz < 4005) ans = find_pre<4005>(acycG, multip); else if (sccs_sz < 4505) ans = find_pre<4505>(acycG, multip); else if (sccs_sz < 5005) ans = find_pre<5005>(acycG, multip); else if (sccs_sz < 5505) ans = find_pre<5505>(acycG, multip); else if (sccs_sz < 6005) ans = find_pre<6005>(acycG, multip); else if (sccs_sz < 7005) ans = find_pre<7005>(acycG, multip); else if (sccs_sz < 8005) ans = find_pre<8005>(acycG, multip); else if (sccs_sz < 9005) ans = find_pre<9005>(acycG, multip); else if (sccs_sz < 10005) ans = find_pre<10005>(acycG, multip); else if (sccs_sz < 12005) ans = find_pre<12005>(acycG, multip); else if (sccs_sz < 14005) ans = find_pre<14005>(acycG, multip); else if (sccs_sz < 16005) ans = find_pre<16005>(acycG, multip); else if (sccs_sz < 18005) ans = find_pre<18005>(acycG, multip); else if (sccs_sz < 20005) ans = find_pre<20005>(acycG, multip); else if (sccs_sz < 25005) ans = find_pre<25005>(acycG, multip); else if (sccs_sz < 30005) ans = find_pre<30005>(acycG, multip); else if (sccs_sz < 50005) ans = find_pre<50005>(acycG, multip); else ans = find_pre<100005>(acycG, multip); rep(i, n) cout << ans[where[i]] << ' '; cout << '\n'; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); return 0; } |