#include <bits/stdc++.h>
using namespace std;
template<class c> struct rge { c b, e; };
template<class c> rge<c> range(c i, c j) { return rge<c>{i, j}; }
template<class c> auto dud(c* x) -> decltype(cerr << *x, 0);
template<class c> char dud(...);
struct debug {
~debug() {
cerr << endl;
}
template<class c> typename \
enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) {
cerr << boolalpha << i;
return *this;
}
template<class c> typename \
enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) {
return *this << range(begin(i), end(i));
}
template<class c, class b> debug & operator <<(pair<b, c> d) {
return *this << "(" << d.first << ", " << d.second << ")";
}
template<class c> debug & operator <<(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it) {
*this << ", " + 2 * (it == d.b) << *it;
}
return *this << "]";
}
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
constexpr int MAXN = 100'007;
long long r[MAXN];
vector<pair<int, long long>> graph[MAXN];
priority_queue<pair<long long, int>> q;
long long max_range[MAXN];
bool queued[MAXN];
void dfs(int u, long long range, int parent = 0) {
assert(range > max_range[u]);
max_range[u] = max(max_range[u], range);
for (auto [v, w] : graph[u]) {
if (w <= range && v != parent && range - w > max_range[v]) {
if (!queued[v]) {
if (r[v] > range - w) {
q.emplace(r[v], v);
}
queued[v] = true;
}
if (range - w >= r[v]) {
dfs(v, range - w, u);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int u = 1; u <= n; u++) {
cin >> r[u];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
long long w;
cin >> u >> v >> w;
graph[u].emplace_back(v, w);
graph[v].emplace_back(u, w);
}
mt19937 gen(2137);
for (int u = 1; u <= n; u++) {
random_shuffle(graph[u].begin(), graph[u].end());
}
for (int u = 1; u <= n; u++) {
fill(max_range + 1, max_range + n + 1, -1);
fill(queued + 1, queued + n + 1, false);
q.emplace(r[u], u);
queued[u] = true;
while (!q.empty()) {
int v = q.top().second;
q.pop();
if (r[v] > max_range[v]) {
dfs(v, r[v]);
}
}
int result = 0;
for (int v = 1; v <= n; v++) {
if (queued[v]) {
result++;
}
}
cout << result << (u == n ? '\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 | #include <bits/stdc++.h> using namespace std; template<class c> struct rge { c b, e; }; template<class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template<class c> auto dud(c* x) -> decltype(cerr << *x, 0); template<class c> char dud(...); struct debug { ~debug() { cerr << endl; } template<class c> typename \ enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template<class c> typename \ enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template<class c, class b> debug & operator <<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template<class c> debug & operator <<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) { *this << ", " + 2 * (it == d.b) << *it; } return *this << "]"; } }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " constexpr int MAXN = 100'007; long long r[MAXN]; vector<pair<int, long long>> graph[MAXN]; priority_queue<pair<long long, int>> q; long long max_range[MAXN]; bool queued[MAXN]; void dfs(int u, long long range, int parent = 0) { assert(range > max_range[u]); max_range[u] = max(max_range[u], range); for (auto [v, w] : graph[u]) { if (w <= range && v != parent && range - w > max_range[v]) { if (!queued[v]) { if (r[v] > range - w) { q.emplace(r[v], v); } queued[v] = true; } if (range - w >= r[v]) { dfs(v, range - w, u); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int u = 1; u <= n; u++) { cin >> r[u]; } for (int i = 0; i < n - 1; i++) { int u, v; long long w; cin >> u >> v >> w; graph[u].emplace_back(v, w); graph[v].emplace_back(u, w); } mt19937 gen(2137); for (int u = 1; u <= n; u++) { random_shuffle(graph[u].begin(), graph[u].end()); } for (int u = 1; u <= n; u++) { fill(max_range + 1, max_range + n + 1, -1); fill(queued + 1, queued + n + 1, false); q.emplace(r[u], u); queued[u] = true; while (!q.empty()) { int v = q.top().second; q.pop(); if (r[v] > max_range[v]) { dfs(v, r[v]); } } int result = 0; for (int v = 1; v <= n; v++) { if (queued[v]) { result++; } } cout << result << (u == n ? '\n' : ' '); } return 0; } |
English