#include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int inf = 1e9 + 2137; const int maxN = 50 * 1000; const int maxB = 5000; ll rad[maxN]; vector<pair<int, ll> > g[maxN]; vector<int> sg[20 * maxN]; bitset<maxB> bs[20 * maxN]; int ans[maxN]; bitset<20 * maxN> reseted; int answered[20 * maxN]; int N; vi val, comp, z, cont; int Time, ncomps; int dfs(int j) { int low = val[j] = ++Time, x; z.push_back(j); for (auto e : sg[j]) if (comp[e] < 0) low = min(low, val[e] ?: dfs(e)); if (low == val[j]) { do { x = z.back(); z.pop_back(); comp[x] = ncomps; cont.push_back(x); } while (x != j); cont.clear(); ncomps++; } return val[j] = low; } void scc() { int n = N; val.assign(n, 0); comp.assign(n, -1); Time = ncomps = 0; rep(i,n) if (comp[i] < 0) dfs(i); } bool dead[maxN]; int sub[maxN]; void prep(int v, int p = -1) { sub[v] = 1; for (auto [u, w] : g[v]) if (!dead[u] && u != p) { prep(u, v); sub[v] += sub[u]; } } int center(int v, int r, int p = -1) { for (auto [u, w] : g[v]) if (!dead[u] && u != p && sub[u] * 2 > sub[r]) return center(u, r, v); return v; } void zbierz(int v, vector<pair<long long, int> >& ziomki, int p = -1, ll d = 0) { ziomki.push_back(make_pair(d, v)); for (auto [u, w] : g[v]) if (!dead[u] && u != p) zbierz(u, ziomki, v, d + w); } void centroid(int root) { prep(root); root = center(root, root); vector<pair<long long, int> > ziomki; zbierz(root, ziomki); sort(all(ziomki)); rep(i, sz(ziomki)) { sg[N + i].push_back(ziomki[i].nd); int p = upper_bound(all(ziomki), make_pair(rad[ziomki[i].nd] - ziomki[i].st, inf)) - begin(ziomki); if (p != 0) sg[ziomki[i].nd].push_back(N + p - 1); } fwd(i, 1, sz(ziomki)) sg[N + i].push_back(N + i - 1); N += sz(ziomki); dead[root] = true; for (auto [v, w] : g[root]) if (!dead[v]) centroid(v); } int32_t main() { fastio; int n; cin >> n; N = n; rep(i, n) cin >> rad[i]; rep(i, n - 1) { int v, u; ll w; cin >> v >> u >> w; -- v, -- u; g[v].push_back(make_pair(u, w)); g[u].push_back(make_pair(v, w)); } centroid(0); scc(); vector<pii> dudes(N); rep(i, N) dudes[i] = make_pair(comp[i], i); sort(all(dudes)); rep(it, (n + maxB - 1) / maxB) { for (auto [c, v] : dudes) { if (v < n && v >= maxB * it && v < maxB * (it + 1)) bs[c][v - maxB * it].flip(); for (auto u : sg[v]) if (comp[u] != c) bs[c] |= bs[comp[u]]; } rep(i, N) { reseted[i] = false; answered[i] = -1; } rep(i, n) { if (answered[comp[i]] == -1) answered[comp[i]] = bs[comp[i]].count(); ans[i] += answered[comp[i]]; } for (auto [c, v] : dudes) if (!reseted[c]) { bs[c].reset(); reseted[c] = true; } } rep(i, n) cout << ans[i] << ' '; cout << '\n'; 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 | #include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int inf = 1e9 + 2137; const int maxN = 50 * 1000; const int maxB = 5000; ll rad[maxN]; vector<pair<int, ll> > g[maxN]; vector<int> sg[20 * maxN]; bitset<maxB> bs[20 * maxN]; int ans[maxN]; bitset<20 * maxN> reseted; int answered[20 * maxN]; int N; vi val, comp, z, cont; int Time, ncomps; int dfs(int j) { int low = val[j] = ++Time, x; z.push_back(j); for (auto e : sg[j]) if (comp[e] < 0) low = min(low, val[e] ?: dfs(e)); if (low == val[j]) { do { x = z.back(); z.pop_back(); comp[x] = ncomps; cont.push_back(x); } while (x != j); cont.clear(); ncomps++; } return val[j] = low; } void scc() { int n = N; val.assign(n, 0); comp.assign(n, -1); Time = ncomps = 0; rep(i,n) if (comp[i] < 0) dfs(i); } bool dead[maxN]; int sub[maxN]; void prep(int v, int p = -1) { sub[v] = 1; for (auto [u, w] : g[v]) if (!dead[u] && u != p) { prep(u, v); sub[v] += sub[u]; } } int center(int v, int r, int p = -1) { for (auto [u, w] : g[v]) if (!dead[u] && u != p && sub[u] * 2 > sub[r]) return center(u, r, v); return v; } void zbierz(int v, vector<pair<long long, int> >& ziomki, int p = -1, ll d = 0) { ziomki.push_back(make_pair(d, v)); for (auto [u, w] : g[v]) if (!dead[u] && u != p) zbierz(u, ziomki, v, d + w); } void centroid(int root) { prep(root); root = center(root, root); vector<pair<long long, int> > ziomki; zbierz(root, ziomki); sort(all(ziomki)); rep(i, sz(ziomki)) { sg[N + i].push_back(ziomki[i].nd); int p = upper_bound(all(ziomki), make_pair(rad[ziomki[i].nd] - ziomki[i].st, inf)) - begin(ziomki); if (p != 0) sg[ziomki[i].nd].push_back(N + p - 1); } fwd(i, 1, sz(ziomki)) sg[N + i].push_back(N + i - 1); N += sz(ziomki); dead[root] = true; for (auto [v, w] : g[root]) if (!dead[v]) centroid(v); } int32_t main() { fastio; int n; cin >> n; N = n; rep(i, n) cin >> rad[i]; rep(i, n - 1) { int v, u; ll w; cin >> v >> u >> w; -- v, -- u; g[v].push_back(make_pair(u, w)); g[u].push_back(make_pair(v, w)); } centroid(0); scc(); vector<pii> dudes(N); rep(i, N) dudes[i] = make_pair(comp[i], i); sort(all(dudes)); rep(it, (n + maxB - 1) / maxB) { for (auto [c, v] : dudes) { if (v < n && v >= maxB * it && v < maxB * (it + 1)) bs[c][v - maxB * it].flip(); for (auto u : sg[v]) if (comp[u] != c) bs[c] |= bs[comp[u]]; } rep(i, N) { reseted[i] = false; answered[i] = -1; } rep(i, n) { if (answered[comp[i]] == -1) answered[comp[i]] = bs[comp[i]].count(); ans[i] += answered[comp[i]]; } for (auto [c, v] : dudes) if (!reseted[c]) { bs[c].reset(); reseted[c] = true; } } rep(i, n) cout << ans[i] << ' '; cout << '\n'; return 0; } |