#include<bits/stdc++.h> using namespace std; using lld = long long; void dfs(vector<vector<pair<int,lld>>>& g, int v, int d, vector<int>& l, int f) { for(auto [u,w] : g[v]) if(u != f && d >= w) { l.push_back(u); dfs(g, u, d-w, l, v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<lld> t(n); for(lld& i : t) cin >> i; vector<vector<pair<int,lld>>> g(n); for(int i=0;i<n-1;i++) { int a,b; lld w; cin >> a >> b >> w, a--, b--; g[a].push_back({b,w}); g[b].push_back({a,w}); } vector<vector<int>> s(n); for(int i=0;i<n;i++) dfs(g, i, t[i], s[i], -1); vector<int> result(n); for(int i=0;i<n;i++) { vector<bool> vis(n, false); queue<int> q; int r = 0; vis[i] = true; q.push(i); while(!q.empty()) { int v = q.front(); q.pop(); r++; for(int u : s[v]) if(!vis[u]) { vis[u] = true; q.push(u); } } result[i] = r; } for(int i : result) cout << 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 | #include<bits/stdc++.h> using namespace std; using lld = long long; void dfs(vector<vector<pair<int,lld>>>& g, int v, int d, vector<int>& l, int f) { for(auto [u,w] : g[v]) if(u != f && d >= w) { l.push_back(u); dfs(g, u, d-w, l, v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<lld> t(n); for(lld& i : t) cin >> i; vector<vector<pair<int,lld>>> g(n); for(int i=0;i<n-1;i++) { int a,b; lld w; cin >> a >> b >> w, a--, b--; g[a].push_back({b,w}); g[b].push_back({a,w}); } vector<vector<int>> s(n); for(int i=0;i<n;i++) dfs(g, i, t[i], s[i], -1); vector<int> result(n); for(int i=0;i<n;i++) { vector<bool> vis(n, false); queue<int> q; int r = 0; vis[i] = true; q.push(i); while(!q.empty()) { int v = q.front(); q.pop(); r++; for(int u : s[v]) if(!vis[u]) { vis[u] = true; q.push(u); } } result[i] = r; } for(int i : result) cout << i << " "; cout << "\n"; return 0; } |