#include <iostream> #include <queue> #include <limits> #include <vector> #include <functional> using namespace std; struct Edge { int t; long long d; }; void dfs(int v, int p, long long acc_d, const vector<vector<Edge>>&G, function<void(int, long long)> accept) { accept(v, acc_d); for (auto nei : G[v]) if (nei.t != p) dfs(nei.t, v, acc_d + nei.d, G, accept); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<long long>R(n); for (long long& r : R) cin >> r; vector<vector<Edge>>G(n); for (int i = 1; i < n; i++) { int x, y; long long d; cin >> x >> y >> d; G[x - 1].push_back(Edge{y - 1, d}); G[y - 1].push_back(Edge{x - 1, d}); } vector<vector<int>> OneStepDet(n); for (int i = 0; i < n; i++) dfs(i, -1, 0, G, [&](int v, long long dist) { if (dist <= R[i]) OneStepDet[i].push_back(v); }); for (int i = 0; i < n; i++) { vector<bool>Det(n); queue<int>ToDetQ; Det[i] = true; ToDetQ.push(i); int result = 0; while (!ToDetQ.empty()) { result++; int v = ToDetQ.front(); ToDetQ.pop(); for (int vv : OneStepDet[v]) if (!Det[vv]) { Det[vv] = true; ToDetQ.push(vv); } } cout << result << " "; } 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 | #include <iostream> #include <queue> #include <limits> #include <vector> #include <functional> using namespace std; struct Edge { int t; long long d; }; void dfs(int v, int p, long long acc_d, const vector<vector<Edge>>&G, function<void(int, long long)> accept) { accept(v, acc_d); for (auto nei : G[v]) if (nei.t != p) dfs(nei.t, v, acc_d + nei.d, G, accept); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<long long>R(n); for (long long& r : R) cin >> r; vector<vector<Edge>>G(n); for (int i = 1; i < n; i++) { int x, y; long long d; cin >> x >> y >> d; G[x - 1].push_back(Edge{y - 1, d}); G[y - 1].push_back(Edge{x - 1, d}); } vector<vector<int>> OneStepDet(n); for (int i = 0; i < n; i++) dfs(i, -1, 0, G, [&](int v, long long dist) { if (dist <= R[i]) OneStepDet[i].push_back(v); }); for (int i = 0; i < n; i++) { vector<bool>Det(n); queue<int>ToDetQ; Det[i] = true; ToDetQ.push(i); int result = 0; while (!ToDetQ.empty()) { result++; int v = ToDetQ.front(); ToDetQ.pop(); for (int vv : OneStepDet[v]) if (!Det[vv]) { Det[vv] = true; ToDetQ.push(vv); } } cout << result << " "; } cout << "\n"; return 0; } |