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
| #include <iostream> #include <cstring> #include <string> #include <vector> #include <queue> #include <algorithm> #include <cstdio> #include <cmath> #define ll long long #define max(x, y) ((x)>(y)?(x):(y)) using namespace std; const int N = 6e5 + 5; const int Z = 6e5 + 5; int n, m; int he[N], ver[N], ne[N], tot; int dep1[N], deep; int d[N]; int q[N], qh, ql; int f[N][30]; inline void add(int x, int y) { ver[++tot] = y; ne[tot] = he[x]; he[x] = tot; } inline int idn(int n) {return n + 3e5+5;} inline int dep2(int n) {return deep - dep1[n];} void bfs() { d[1] = 1; q[++qh] = 1; while (qh > ql) { int te = q[++ql]; for (int i = he[te]; i; i = ne[i]) { int v = ver[i]; if (d[v]) continue; d[v] = d[te] + 1; f[v][0] = te; for (int j = 1; j < 30; j++) f[v][j] = f[f[v][j - 1]][j - 1]; dep1[v] = dep1[te] + 1; deep = max(dep1[v], deep); q[++qh] = v; } } } int lca(int x, int y) { if (d[x] > d[y]) swap(x, y); for (int i = 29; i >= 0; i--) { if (d[f[y][i]] < d[x]) continue; y = f[y][i]; } if (x == y) return x; for (int i = 29; i >= 0; i--) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; return f[x][0]; } struct Node { int l, r; int val; }tr[N * 40]; int cnt, top; int rt[N], bac[N*40]; inline int newnod() { return cnt ? bac[cnt--] : ++top; } inline void del(int p) { bac[++cnt] = p; tr[p].l = tr[p].r = tr[p].val = 0; } void insert(int &p, int pos, int v, int l = 1, int r = Z) { if (!p) p = newnod(); tr[p].val += v; if (l == r) return; int mid = (l + r) >> 1; if (pos <= mid) insert(tr[p].l, pos, v, l, mid); else insert(tr[p].r, pos, v, mid + 1, r); } int merge(int x, int y, int l = 1, int r = Z) { if (!x || !y) return x + y; tr[x].val += tr[y].val; int mid = (l + r) >> 1; tr[x].l = merge(tr[x].l, tr[y].l, l, mid); tr[x].r = merge(tr[x].r, tr[y].r, mid + 1, r); del(y); return x; } int qt[N], ans[N]; bool flag = 0; int ask(int p, int k, int l = 1, int r = Z) { if (l == r) return tr[p].val; int mid = (l + r) >> 1; if (k <= mid) return ask(tr[p].l, k, l, mid); else return ask(tr[p].r, k, mid+1, r); } void dfs_mg(int cur, int fa) { for (int i = he[cur]; i; i = ne[i]) { int y = ver[i]; if (y == fa) continue; dfs_mg(y, cur); rt[cur] = merge(rt[cur], rt[y]); } int tt = qt[cur]; ans[cur] += ask(rt[cur], idn(tt - (flag ? dep1[cur] : dep2(cur)))); return; } struct qy { int lca, y; int lca_m, y_m; }qr[N]; int main() { cin >> n >> m; for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); add(x, y); add(y, x); } bfs(); for (int i = 1; i <= n; i++) scanf("%d", &qt[i]); for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); int _lca = lca(x, y); insert(rt[x], idn(0-dep2(x)), 1); insert(rt[f[_lca][0]], idn(0-dep2(x)), -1); qr[i].lca = _lca; qr[i].y = y; qr[i].y_m = (dep1[x] - dep1[_lca]) + (dep1[y] - dep1[_lca]); } dfs_mg(1, 0); cnt = top = 0; for (int i = 0; i < N * 40; i++) tr[i].l = tr[i].r = tr[i].val = 0; memset(rt, 0, sizeof(rt)); for (int i = 1; i <= m; i++) { int y = qr[i].y, _lca = qr[i].lca; int tt = qr[i].y_m; insert(rt[y], idn(tt-dep1[y]), 1); insert(rt[_lca], idn(tt-dep1[y]), -1); } flag = 1; dfs_mg(1, 0); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); puts(""); return 0; }
|