題意 "題目鏈接" Sol 一步一步的來考慮 $25 \%$:直接$O(nm)$的暴力 鏈的情況:維護兩個差分數組,分別表示從左向右和從右向左的貢獻, $S_i = 1$:統計每個點的子樹內有多少起點即可 $T_i = 1$:同樣還是差分的思想,由於每個點 能對其產生的點的深度是相同的(假設為$x$ ...
題意
Sol
一步一步的來考慮
\(25 \%\):直接\(O(nm)\)的暴力
鏈的情況:維護兩個差分數組,分別表示從左向右和從右向左的貢獻,
\(S_i = 1\):統計每個點的子樹內有多少起點即可
\(T_i = 1\):同樣還是差分的思想,由於每個點 能對其產生的點的深度是相同的(假設為\(x\)),那麼訪問該點時記錄下\(dep[x]\)的數量,將結束時\(dep[x]\)的數量與其做差即可
滿分做法和上面類似,我們考慮把每個點的貢獻都轉換到子樹內統計
對於每次詢問,拆為\(S->lca, lca -> T\)兩種(從下到上 / 從上到下)
從上往下需要滿足的條件:\(dep[i] - w[i] = dep[T] - len\)
從下往上需要滿足的條件:\(dep[i] + w[i] = dep[s]\)
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, B = 20;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, ans[MAXN], dep[MAXN], top[MAXN], son[MAXN], siz[MAXN], fa[MAXN], S[MAXN],
T[MAXN], w[MAXN], tmp[MAXN], num2[MAXN], sum1[MAXN], sum2[MAXN], Lca[MAXN];
int *num1;//上 -> 下
vector<int> up[MAXN], da[MAXN], dc[MAXN];
vector<int> v[MAXN];
void dfs(int x, int _fa) {
dep[x] = dep[_fa] + 1; siz[x] = 1; fa[x] = _fa;
for(int i = 0, to; i < v[x].size(); i++) {
if((to = v[x][i]) == _fa) continue;
dfs(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
}
void dfs2(int x, int topf) {
top[x] = topf;
if(!son[x]) return ;
dfs2(son[x], topf);
for(int i = 0, to; i < v[x].size(); i++)
if(!top[to = v[x][i]]) dfs2(to, to);
}
int LCA(int x, int y) {
while(top[x] ^ top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
void Deal(int s, int t, int id) {// from s to t
int lca = LCA(s, t); Lca[id] = lca;
up[lca].push_back(s);//from down to up
int dis = dep[s] + dep[t] - 2 * dep[lca];
sum2[s]++;
da[t].push_back(dep[t] - dis);//increase
dc[lca].push_back(dep[t] - dis);//decrase
}
void Find(int x) {
int t1 = num1[dep[x] - w[x]], t2 = num2[dep[x] + w[x]];// 1: 從上往下 2:從下往上
for(int i = 0, to; i < v[x].size(); i++) {
if((to = v[x][i]) == fa[x]) continue;
Find(to);
}
num2[dep[x]] += sum2[x];
for(int i = 0; i < da[x].size(); i++) num1[da[x][i]]++;
ans[x] += num2[dep[x] + w[x]] - t2 + num1[dep[x] - w[x]] - t1;
for(int i = 0; i < up[x].size(); i++) num2[dep[up[x][i]]]--;
for(int i = 0; i < dc[x].size(); i++) num1[dc[x][i]]--;
}
int main() {
//freopen("a.in", "r", stdin); freopen("a.out", "w", stdout);
num1 = tmp + (int)3e5 + 10;
N = read(); M = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dep[0] = -1; dfs(1, 0); dfs2(1, 1);
//for(int i = 1; i <= N; i++, puts("")) for(int j = 1; j <= N; j++) printf("%d %d %d\n", i, j, LCA(i, j));
for(int i = 1; i <= N; i++) w[i] = read();
for(int i = 1; i <= M; i++) S[i] = read(), T[i] = read(), Deal(S[i], T[i], i);
Find(1);
for(int i = 1; i <= M; i++) if(dep[S[i]] - dep[Lca[i]] == w[Lca[i]]) ans[Lca[i]]--;
for(int i = 1; i <= N; i++) printf("%d ", ans[i]);
return 0;
}