#include <stdio.h> #include <vector> using ll=long long; using namespace std; const int C=150001; vector <vector <int> > gr(C); //Proper bfs int _bfs_par[C], _bfs_dist[C], _bfs_fifo[C]; bool _bfs_visited[C]; void bfs(vector <vector<int>>& gr, int s, int n){ int iF=0, jF=1, i, j, a, b, Inf=1000000001; _bfs_fifo[0] = s; for (i=1; i<=n; i++) _bfs_dist[i] = Inf; for (i=1; i<=n; i++) _bfs_visited[i] = false; _bfs_dist[s] = 0; _bfs_visited[s] = 1; for (; iF<jF; iF++){ a = _bfs_fifo[iF]; for (j=0; j<gr[a].size(); j++){ b=gr[a][j]; if (_bfs_visited[b]) continue; _bfs_visited[b] = true; _bfs_par[b] = a; _bfs_dist[b] = _bfs_dist[a]+1; _bfs_fifo[jF] = b; jF++; } } } ll v[C]; vector <vector <ll> > edge(C); int s[C], is=0, par[C], ji[C]; ll s_weight[C]; void proc_tree(int a, int n, vector <vector <int> > &tab, ll v){ s[0]=a, is=1; int b, root = a; s_weight[0] = v; for(int z=1; z<=n; z++) ji[z]=tab[z].size(), par[z]=0; while (is>0){ a=s[is-1]; ll cur_weight = s_weight[is-1]; if (ji[a]>0 && tab[a][ji[a]-1]==par[a]) ji[a]--; if (ji[a]>0){ b=tab[a][ji[a]-1]; ll weight = edge[a][ji[a]-1]; if (cur_weight >= weight){ s[is] = b; gr[root].push_back(b); s_weight[is] = cur_weight-weight; par[b] = a; is++; } ji[a]--; } else is--; } } vector <vector <int> > tree(C); int main(){ int n; scanf ("%d", &n); for (int i=1; i<=n; i++) scanf ("%lld", &v[i]); for (int i=0; i<n-1; i++){ ll e; int a, b; scanf ("%d %d %lld", &a, &b, &e); tree[a].push_back(b); tree[b].push_back(a); edge[a].push_back(e); edge[b].push_back(e); } for (int i=1; i<=n; i++) proc_tree(i, n, tree, v[i]); for (int i=1; i<=n; i++){ bfs(gr, i, n); ll res=0; for (int j=1; j<=n; j++){ if (_bfs_visited[j] == true) res++; } printf ("%lld ", res); } printf ("\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 | #include <stdio.h> #include <vector> using ll=long long; using namespace std; const int C=150001; vector <vector <int> > gr(C); //Proper bfs int _bfs_par[C], _bfs_dist[C], _bfs_fifo[C]; bool _bfs_visited[C]; void bfs(vector <vector<int>>& gr, int s, int n){ int iF=0, jF=1, i, j, a, b, Inf=1000000001; _bfs_fifo[0] = s; for (i=1; i<=n; i++) _bfs_dist[i] = Inf; for (i=1; i<=n; i++) _bfs_visited[i] = false; _bfs_dist[s] = 0; _bfs_visited[s] = 1; for (; iF<jF; iF++){ a = _bfs_fifo[iF]; for (j=0; j<gr[a].size(); j++){ b=gr[a][j]; if (_bfs_visited[b]) continue; _bfs_visited[b] = true; _bfs_par[b] = a; _bfs_dist[b] = _bfs_dist[a]+1; _bfs_fifo[jF] = b; jF++; } } } ll v[C]; vector <vector <ll> > edge(C); int s[C], is=0, par[C], ji[C]; ll s_weight[C]; void proc_tree(int a, int n, vector <vector <int> > &tab, ll v){ s[0]=a, is=1; int b, root = a; s_weight[0] = v; for(int z=1; z<=n; z++) ji[z]=tab[z].size(), par[z]=0; while (is>0){ a=s[is-1]; ll cur_weight = s_weight[is-1]; if (ji[a]>0 && tab[a][ji[a]-1]==par[a]) ji[a]--; if (ji[a]>0){ b=tab[a][ji[a]-1]; ll weight = edge[a][ji[a]-1]; if (cur_weight >= weight){ s[is] = b; gr[root].push_back(b); s_weight[is] = cur_weight-weight; par[b] = a; is++; } ji[a]--; } else is--; } } vector <vector <int> > tree(C); int main(){ int n; scanf ("%d", &n); for (int i=1; i<=n; i++) scanf ("%lld", &v[i]); for (int i=0; i<n-1; i++){ ll e; int a, b; scanf ("%d %d %lld", &a, &b, &e); tree[a].push_back(b); tree[b].push_back(a); edge[a].push_back(e); edge[b].push_back(e); } for (int i=1; i<=n; i++) proc_tree(i, n, tree, v[i]); for (int i=1; i<=n; i++){ bfs(gr, i, n); ll res=0; for (int j=1; j<=n; j++){ if (_bfs_visited[j] == true) res++; } printf ("%lld ", res); } printf ("\n"); return 0;} |