#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; i >= a; i--) #define cat(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; const int N = 100100; const int M = 17; int n, dep[N], jmp[N][M], res[N], time_euler[N], clz[N]; long long r[N], d[N], rmq[N][M]; vector<pair<int, long long>> e[N]; vector<int> E[N], R[N]; vector<long long> euler; void dfs(int u, int p) { dep[u] = dep[p] + 1; time_euler[u] = euler.size(); euler.push_back(d[u]); for (auto [v, w] : e[u]) { if (v != p) { d[v] = d[u] + w; dfs(v, u); euler.push_back(d[u]); } } } long long query(int a, int b) { if (a > b) swap(a, b); int k = clz[b - a + 1]; return min(rmq[a][k], rmq[b - (1 << k) + 1][k]); } long long dist(int a, int b) { return d[a] + d[b] - 2 * query(time_euler[a], time_euler[b]); } vector<int> order; int vis[N], id[N]; vector<int> nodes[N], edges[N]; void dfs1(int u) { vis[u] = 1; for (auto v : E[u]) { if (!vis[v]) { dfs1(v); } } order.push_back(u); } void dfs2(int u, int ID) { vis[u] = 1; id[u] = ID; nodes[ID].push_back(u); for (auto v : R[u]) { if (!vis[v]) { dfs2(v, ID); } } } int solve(int c) { vis[c] = 1; int res = nodes[c].size(); for (auto d : edges[c]) { if (!vis[d]) { res += solve(d); } } return res; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; rep(i, 1, n) cin >> r[i]; rep(i, 2, n) { int a, b; long long c; cin >> a >> b >> c; e[a].push_back({b, c}); e[b].push_back({a, c}); } dfs(1, 0); int m = euler.size(); assert(m == 2 * (n - 1) + 1); for (int i = 1; i <= m; i++) { clz[i] = 31 - __builtin_clz(i); } for (int i = 0; i < m; i++) { rmq[i][0] = euler[i]; } for (int j = 0; j + 1 < M; j++) { for (int i = 0; i + (1 << (j + 1)) <= m; i++) { rmq[i][j + 1] = min(rmq[i][j], rmq[i + (1 << j)][j]); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { long long D = dist(i, j); if (D <= r[i]) { E[i].push_back(j); R[j].push_back(i); } if (D <= r[j]) { E[j].push_back(i); R[i].push_back(j); } } } rep(i, 1, n) if (!vis[i]) dfs1(i); rep(i, 1, n) vis[i] = 0; reverse(order.begin(), order.end()); int cnt = 0; for (auto u : order) { if (!vis[u]) { dfs2(u, ++cnt); } } rep(u, 1, n) { for (auto v : E[u]) { edges[id[u]].push_back(id[v]); } } rep(c, 1, cnt) { sort(edges[c].begin(), edges[c].end()); edges[c].erase(unique(edges[c].begin(), edges[c].end()), edges[c].end()); } rep(c, 1, cnt) { rep(i, 1, cnt) vis[i] = 0; int now = solve(c); for (auto u : nodes[c]) { res[u] = now; } } rep(i, 1, n) { cout << res[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 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; i >= a; i--) #define cat(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; const int N = 100100; const int M = 17; int n, dep[N], jmp[N][M], res[N], time_euler[N], clz[N]; long long r[N], d[N], rmq[N][M]; vector<pair<int, long long>> e[N]; vector<int> E[N], R[N]; vector<long long> euler; void dfs(int u, int p) { dep[u] = dep[p] + 1; time_euler[u] = euler.size(); euler.push_back(d[u]); for (auto [v, w] : e[u]) { if (v != p) { d[v] = d[u] + w; dfs(v, u); euler.push_back(d[u]); } } } long long query(int a, int b) { if (a > b) swap(a, b); int k = clz[b - a + 1]; return min(rmq[a][k], rmq[b - (1 << k) + 1][k]); } long long dist(int a, int b) { return d[a] + d[b] - 2 * query(time_euler[a], time_euler[b]); } vector<int> order; int vis[N], id[N]; vector<int> nodes[N], edges[N]; void dfs1(int u) { vis[u] = 1; for (auto v : E[u]) { if (!vis[v]) { dfs1(v); } } order.push_back(u); } void dfs2(int u, int ID) { vis[u] = 1; id[u] = ID; nodes[ID].push_back(u); for (auto v : R[u]) { if (!vis[v]) { dfs2(v, ID); } } } int solve(int c) { vis[c] = 1; int res = nodes[c].size(); for (auto d : edges[c]) { if (!vis[d]) { res += solve(d); } } return res; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; rep(i, 1, n) cin >> r[i]; rep(i, 2, n) { int a, b; long long c; cin >> a >> b >> c; e[a].push_back({b, c}); e[b].push_back({a, c}); } dfs(1, 0); int m = euler.size(); assert(m == 2 * (n - 1) + 1); for (int i = 1; i <= m; i++) { clz[i] = 31 - __builtin_clz(i); } for (int i = 0; i < m; i++) { rmq[i][0] = euler[i]; } for (int j = 0; j + 1 < M; j++) { for (int i = 0; i + (1 << (j + 1)) <= m; i++) { rmq[i][j + 1] = min(rmq[i][j], rmq[i + (1 << j)][j]); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { long long D = dist(i, j); if (D <= r[i]) { E[i].push_back(j); R[j].push_back(i); } if (D <= r[j]) { E[j].push_back(i); R[i].push_back(j); } } } rep(i, 1, n) if (!vis[i]) dfs1(i); rep(i, 1, n) vis[i] = 0; reverse(order.begin(), order.end()); int cnt = 0; for (auto u : order) { if (!vis[u]) { dfs2(u, ++cnt); } } rep(u, 1, n) { for (auto v : E[u]) { edges[id[u]].push_back(id[v]); } } rep(c, 1, cnt) { sort(edges[c].begin(), edges[c].end()); edges[c].erase(unique(edges[c].begin(), edges[c].end()), edges[c].end()); } rep(c, 1, cnt) { rep(i, 1, cnt) vis[i] = 0; int now = solve(c); for (auto u : nodes[c]) { res[u] = now; } } rep(i, 1, n) { cout << res[i] << " "; } cout << "\n"; return 0; } |