#include <bits/stdc++.h>
#define dbg(x) " [" << #x << ": " << (x) << "] "
using namespace std;
template<typename A, typename B>
ostream& operator<<(ostream& out, const pair<A,B>& p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& c) {
out << "{";
for(auto it = c.begin(); it != c.end(); it++) {
if(it != c.begin()) out << ", ";
out << *it;
}
return out << "}";
}
vector<long long> r;
vector<vector<pair<int,long long>>> g;
vector<vector<int>> gg;
int cnt;
int cur;
vector<int> ord, u;
void dfs(int v, long long d) {
gg[cur].push_back(v);
u[v] = 1;
ord.push_back(v);
for(auto [to, c] : g[v]) {
if(u[to] || d + c > r[cur]) continue;
dfs(to, d + c);
}
}
void dfs2(int v) {
u[v] = 1;
cnt++;
ord.push_back(v);
for(int to : gg[v]) {
if(!u[to]) dfs2(to);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
r.resize(n);
g.resize(n);
gg.resize(n);
u.resize(n);
for(auto& i : r) cin >> i;
for(int i = 0; i < n - 1; i++) {
int a,b;
long long c;
cin >> a >> b >> c;
a--;
b--;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
for(int i = 0; i < n; i++) {
for(int v : ord) u[v] = 0;
ord.clear();
cur = i;
dfs(i, 0);
}
for(int i = 0; i < n; i++) {
for(int v : ord) u[v] = 0;
ord.clear();
cnt = 0;
dfs2(i);
if(i) cout << " ";
cout << cnt;
}
cout << endl;
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 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } vector<long long> r; vector<vector<pair<int,long long>>> g; vector<vector<int>> gg; int cnt; int cur; vector<int> ord, u; void dfs(int v, long long d) { gg[cur].push_back(v); u[v] = 1; ord.push_back(v); for(auto [to, c] : g[v]) { if(u[to] || d + c > r[cur]) continue; dfs(to, d + c); } } void dfs2(int v) { u[v] = 1; cnt++; ord.push_back(v); for(int to : gg[v]) { if(!u[to]) dfs2(to); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; r.resize(n); g.resize(n); gg.resize(n); u.resize(n); for(auto& i : r) cin >> i; for(int i = 0; i < n - 1; i++) { int a,b; long long c; cin >> a >> b >> c; a--; b--; g[a].push_back({b, c}); g[b].push_back({a, c}); } for(int i = 0; i < n; i++) { for(int v : ord) u[v] = 0; ord.clear(); cur = i; dfs(i, 0); } for(int i = 0; i < n; i++) { for(int v : ord) u[v] = 0; ord.clear(); cnt = 0; dfs2(i); if(i) cout << " "; cout << cnt; } cout << endl; return 0; } |
English