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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| #include <bits/stdc++.h> #define re register #define int long long using namespace std; const int N = 2e5 + 10,M = 4e5 + 10; int n; int dp[N]; int idx,h[N],ne[M],e[M],w[M],p[N]; int num,f[N],d[N],sz[N],wson[N],id[N],tp[N]; inline int read(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; } inline void add(int a,int b,int c){ ne[idx] = h[a]; e[idx] = b; w[idx] = c; h[a] = idx++; } struct chain{ #define ls(u) (u << 1) #define rs(u) (u << 1 | 1) struct node{ int l,r; int Max,tag; }tr[N << 2]; inline void calc(int u,int k){ tr[u].Max += k; tr[u].tag += k; } inline void pushup(int u){ tr[u].Max = max(tr[ls(u)].Max,tr[rs(u)].Max); } inline void pushdown(int u){ if (tr[u].tag){ calc(ls(u),tr[u].tag); calc(rs(u),tr[u].tag); tr[u].tag = 0; } } inline void build(int u,int l,int r){ tr[u] = {l,r}; if (l == r) return; int mid = l + r >> 1; build(ls(u),l,mid); build(rs(u),mid + 1,r); pushup(u); } inline void modify(int u,int l,int r,int k){ if (l <= tr[u].l && tr[u].r <= r){ calc(u,k); return; } pushdown(u); int mid = tr[u].l + tr[u].r >> 1; if (l <= mid) modify(ls(u),l,r,k); if (r > mid) modify(rs(u),l,r,k); pushup(u); } inline int query(int u,int l,int r){ if (l <= tr[u].l && tr[u].r <= r) return tr[u].Max; pushdown(u); int res = 0; int mid = tr[u].l + tr[u].r >> 1; if (l <= mid) res = max(res,query(ls(u),l,r)); if (r > mid) res = max(res,query(rs(u),l,r)); return res; } inline void modify_node(int x,int k){ modify(1,id[x],id[x],k); } inline void modify_tree(int x,int k){ modify(1,id[x],id[x] + sz[x] - 1,k); } inline int query_sec(int l,int r){ if (l > r) return 0; return query(1,l,r); } #undef ls #undef rs }tree; inline void dfs1(int u,int fa){ sz[u] = 1; f[u] = fa; d[u] = d[fa] + 1; for (re int i = h[u];~i;i = ne[i]){ int j = e[i]; if (j == fa) continue; dfs1(j,u); if (sz[j] > sz[wson[u]]) wson[u] = j; sz[u] += sz[j]; } } inline void dfs2(int u,int fa,int top){ num++; id[u] = num; tp[u] = top; if (!wson[u]) return; dfs2(wson[u],u,top); for (re int i = h[u];~i;i = ne[i]){ int j = e[i]; if (j == fa || j == wson[u]) continue; dfs2(j,u,j); } } inline void dfs_val(int u,int fa,int d){ tree.modify_node(u,d + p[u]); for (re int i = h[u];~i;i = ne[i]){ int j = e[i]; if (j == fa) continue; dfs_val(j,u,d + w[i]); } } inline void dfs_get(int u,int fa){ for (re int i = h[u];~i;i = ne[i]){ int j = e[i]; if (j == fa) continue; tree.modify_tree(1,w[i]); tree.modify_tree(j,-2 * w[i]); dp[j] = max(tree.query_sec(1,id[j] - 1),tree.query_sec(id[j] + 1,n)); dfs_get(j,u); tree.modify_tree(1,-w[i]); tree.modify_tree(j,2 * w[i]); } } signed main(){ memset(h,-1,sizeof(h)); n = read(); for (re int i = 1;i < n;i++){ int a,b,c; a = read(); b = read(); c = read(); add(a,b,c); add(b,a,c); } for (re int i = 1;i <= n;i++) p[i] = read(); dfs1(1,0); dfs2(1,0,1); tree.build(1,1,n); dfs_val(1,0,0); dp[1] = max(tree.query_sec(1,id[1] - 1),tree.query_sec(id[1] + 1,n)); dfs_get(1,0); for (re int i = 1;i <= n;i++) printf("%lld\n",dp[i]); return 0; }
|