#include <bits/stdc++.h> using namespace std; #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=3007; ll r[N]; vector<pair<int,ll>>G[N]; bitset<N>is[N]; ll d[N][N]; bool odw[N]; void dfs(int v,int o,int p) { for(auto u:G[v]) { if(u.st==o) continue; d[p][u.st]=d[p][v]+u.nd; dfs(u.st,v,p); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,a,b; cin>>n; for(int i=1;i<=n;i++) cin>>r[i]; for(int i=1;i<n;i++) { ll c; cin>>a>>b>>c; G[a].pb({b,c}); G[b].pb({a,c}); } for(int i=1;i<=n;i++) { dfs(i,0,i); for(int j=1;j<=n;j++) if(d[i][j]<=r[i]) is[i][j]=1; } for(int i=1;i<=n;i++) { deque<int>Q; bitset<N>no; no.set(); no[i]=0; Q.pb(i); int ans=0; while(sz(Q)>0) { int v=Q[0]; Q.pop_front(); ans++; bitset<N>ne=no&is[v]; int c=ne.count(); while(c--) { int u=ne._Find_first(); no[u]=0; ne[u]=0; Q.pb(u); } } cout<<ans<<" "; } 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 76 77 78 79 80 81 82 83 | #include <bits/stdc++.h> using namespace std; #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=3007; ll r[N]; vector<pair<int,ll>>G[N]; bitset<N>is[N]; ll d[N][N]; bool odw[N]; void dfs(int v,int o,int p) { for(auto u:G[v]) { if(u.st==o) continue; d[p][u.st]=d[p][v]+u.nd; dfs(u.st,v,p); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,a,b; cin>>n; for(int i=1;i<=n;i++) cin>>r[i]; for(int i=1;i<n;i++) { ll c; cin>>a>>b>>c; G[a].pb({b,c}); G[b].pb({a,c}); } for(int i=1;i<=n;i++) { dfs(i,0,i); for(int j=1;j<=n;j++) if(d[i][j]<=r[i]) is[i][j]=1; } for(int i=1;i<=n;i++) { deque<int>Q; bitset<N>no; no.set(); no[i]=0; Q.pb(i); int ans=0; while(sz(Q)>0) { int v=Q[0]; Q.pop_front(); ans++; bitset<N>ne=no&is[v]; int c=ne.count(); while(c--) { int u=ne._Find_first(); no[u]=0; ne[u]=0; Q.pb(u); } } cout<<ans<<" "; } cout<<endl; return 0; } |