#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5 + 5;
inline int read() {
char ch = getchar(); int x = 0;
while (!isdigit(ch)) {ch = getchar();}
while (isdigit(ch)) {x = (x << 3) + (x << 1) + ch - 48; ch = getchar();}
return x;
}
struct node {
ll kf, kg, tag_h, fg, tag_g, tag_f;
node(ll a = 0, ll b = 0, ll c = 0, ll d = 0, ll e = 0, ll f = 0) {kf = a; kg = b; tag_h = c; fg = d; tag_g = e; tag_f = f;}
node operator*(const node &tmp) const
{return node(kf + tmp.kf + tmp.kg * fg, kg + tmp.kg, tag_h + tmp.tag_h + tmp.kf * tag_f + tmp.kg * tag_g, fg + tmp.fg, tag_g + tmp.tag_g + tmp.fg * tag_f, tag_f + tmp.tag_f);}
}tag[N], Tag[N], lst[N];
int n, D, DD, T, ht[N], id[N], top[N], son[N];
ll ans[N], fin;
ll nowf[N], nowg[N], nowh[N];
vector<int> to[N];
vector<ll> f[N], g[N], h[N], F[N], G[N], H[N], lstf[N], lstg[N], lsth[N];
inline ll calc_h(int x, int pos) {return pos = min(pos + id[x], ht[top[x]] - 1), pos < id[x] ? 0 : h[top[x]][pos] + tag[top[x]].tag_h + tag[top[x]].kg * g[top[x]][pos] + tag[top[x]].kf * f[top[x]][pos];}
inline ll calc_g(int x, int pos) {return pos = min(pos + id[x], ht[top[x]] - 1), pos < id[x] ? 0 : g[top[x]][pos] + tag[top[x]].tag_g + tag[top[x]].fg * f[top[x]][pos];}
inline ll calc_f(int x, int pos) {return pos = min(pos + id[x], ht[top[x]] - 1), pos < id[x] ? 0 : f[top[x]][pos] + tag[top[x]].tag_f;}
inline ll calc_H(int x, int pos) {return H[top[x]][pos] + Tag[top[x]].tag_h + Tag[top[x]].kg * G[top[x]][pos] + Tag[top[x]].kf * F[top[x]][pos];}
inline ll calc_G(int x, int pos) {return G[top[x]][pos] + Tag[top[x]].tag_g + Tag[top[x]].fg * F[top[x]][pos];}
inline ll calc_F(int x, int pos) {return F[top[x]][pos] + Tag[top[x]].tag_f;}
void update(int x, int anc) {
top[x] = anc;
if (son[x]) update(son[x], anc);
}
void dfs(int x, int fa, int d) {
id[x] = d; ht[x] = 1;
for (auto y : to[x]) {
if (y == fa) continue;
dfs(y, x, d + 1);
ht[x] = max(ht[x], ht[y] + 1);
if (ht[y] > ht[son[x]]) son[x] = y;
}
for (auto y : to[x]) {
if (y == fa || y == son[x]) continue;
update(y, y);
}
if (x == 1) update(x, x);
}
void dfs1(int x, int fa) {
if (son[x]) dfs1(son[x], x);
lst[x] = tag[top[x]];
int mx = 0;
for (auto y : to[x]) {
if (y == fa || y == son[x]) continue;
mx = max(mx, ht[y]);
}
if (x == top[x]) mx = ht[x];
lstf[x].resize(mx);
lstg[x].resize(mx);
lsth[x].resize(mx);
for (int i = 0; i < mx; ++i) lstf[x][i] = f[top[x]][id[x] + i];
for (int i = 0; i < mx; ++i) lstg[x][i] = g[top[x]][id[x] + i];
for (int i = 0; i < mx; ++i) lsth[x][i] = h[top[x]][id[x] + i];
f[top[x]][id[x]] = -tag[top[x]].tag_f;
g[top[x]][id[x]] = -tag[top[x]].tag_g - tag[top[x]].fg * f[top[x]][id[x]];
h[top[x]][id[x]] = -tag[top[x]].tag_h - tag[top[x]].kg * g[top[x]][id[x]] - tag[top[x]].kf * f[top[x]][id[x]];
node tmp;
tmp = node(1, 1, 0, 1, 0, 1);
tag[top[x]] = tag[top[x]] * tmp;
for (auto y : to[x]) {
if (y == fa || y == son[x]) continue;
dfs1(y, x);
for (int i = 0; i < ht[y]; ++i) {
nowh[i] = calc_h(x, i);
nowg[i] = calc_g(x, i);
nowf[i] = calc_f(x, i);
}
for (int i = 1; i < ht[y]; ++i) {
nowh[i] += h[y][i - 1] + nowg[i] * f[y][i - 1] + nowf[i] * (g[y][i - 1] + f[y][i - 1]);
nowg[i] += g[y][i - 1] + f[y][i - 1];
nowf[i] += f[y][i - 1];
}
tmp = node(g[y][ht[y] - 1] + f[y][ht[y] - 1], f[y][ht[y] - 1], h[y][ht[y] - 1], 0, g[y][ht[y] - 1] + f[y][ht[y] - 1], f[y][ht[y] - 1]);
tag[top[x]] = tag[top[x]] * tmp;
for (int i = 0; i < ht[y]; ++i) {
f[top[x]][id[x] + i] = nowf[i] - tag[top[x]].tag_f;
g[top[x]][id[x] + i] = nowg[i] - tag[top[x]].tag_g - tag[top[x]].fg * f[top[x]][id[x] + i];
h[top[x]][id[x] + i] = nowh[i] - tag[top[x]].tag_h - tag[top[x]].kf * f[top[x]][id[x] + i] - tag[top[x]].kg * g[top[x]][id[x] + i];
}
}
if (x == top[x]) {
for (int i = 0; i < ht[x]; ++i) {
h[x][i] = calc_h(x, i);
g[x][i] = calc_g(x, i);
f[x][i] = calc_f(x, i);
}
tag[x] = node();
}
}
void dfs2(int x, int fa, int Fa) {
if (fa && x == son[fa]) {
node tmp;
int mx = 0; Tag[0] = node();
for (auto y : to[fa])
if (y != Fa && y != x) mx = max(mx, ht[y]);
for (int i = 0; i < mx; ++i) F[0][i] = G[0][i] = H[0][i] = 0;
for (int i = 0; i < D && i < mx; ++i) {
ll vf, vg, vh;
vh = calc_H(fa, i + 1 + id[fa]);
vg = calc_G(fa, i + 1 + id[fa]);
vf = calc_F(fa, i + 1 + id[fa]);
F[0][i] = vf + 1;
G[0][i] = vg + vf;
H[0][i] = vh + vg + vf;
}
for (auto y : to[fa]) {
if (y == Fa) continue;
for (int i = max(0, D - ht[y] - 1); i < D && i < mx; ++i) {
ll uf, ug, uh; int pos = D - i - 2;
uh = calc_h(y, pos);
ug = calc_g(y, pos);
uf = calc_f(y, pos);
nowh[i] = calc_H(0, i);
nowg[i] = calc_G(0, i);
nowf[i] = calc_F(0, i);
nowh[i] += uh + nowg[i] * uf + nowf[i] * (ug + uf);
nowg[i] += ug + uf;
nowf[i] += uf;
}
if (D - ht[y] - 1 > 0) {
ll uf, ug, uh;
uh = calc_h(y, D - 2);
ug = calc_g(y, D - 2);
uf = calc_f(y, D - 2);
tmp = node(ug + uf, uf, uh, 0, ug + uf, uf);
Tag[0] = Tag[0] * tmp;
}
for (int i = max(0, D - ht[y] - 1); i < D && i < mx; ++i) {
F[0][i] = nowf[i] - Tag[0].tag_f;
G[0][i] = nowg[i] - Tag[0].tag_g - Tag[0].fg * F[0][i];
H[0][i] = nowh[i] - Tag[0].tag_h - Tag[0].kg * G[0][i] - Tag[0].kf * F[0][i];
}
}
for (auto y : to[fa]) {
if (y == Fa || y == x) continue;
for (int i = 0; i < D && i < ht[top[y]]; ++i) {
ll uf, ug, uh, vf, vg, vh; int pos = D - i - 2;
uh = calc_h(y, pos);
ug = calc_g(y, pos);
uf = calc_f(y, pos);
vh = calc_H(0, i);
vg = calc_G(0, i);
vf = calc_F(0, i);
F[y][i] = vf - uf;
G[y][i] = vg - uf - ug;
H[y][i] = vh - uh - F[y][i] * (uf + ug) - G[y][i] * uf;
}
}
tmp = node(1, 1, 0, 1, 0, 1);
Tag[top[x]] = Tag[top[x]] * tmp;
for (auto y : to[fa]) {
if (y == Fa || y == x) continue;
for (int i = max(0, D - ht[y] - 1); i < D && i + id[x] < ht[top[x]]; ++i) {
ll uf, ug, uh;
uh = calc_h(y, D - i - 2);
ug = calc_g(y, D - i - 2);
uf = calc_f(y, D - i - 2);
nowh[i] = calc_H(x, id[x] + i);
nowg[i] = calc_G(x, id[x] + i);
nowf[i] = calc_F(x, id[x] + i);
nowh[i] += uh + nowg[i] * uf + nowf[i] * (ug + uf);
nowg[i] += ug + uf;
nowf[i] += uf;
}
if (D - ht[y] - 1 > 0) {
ll uf, ug, uh;
uh = calc_h(y, D - 2);
ug = calc_g(y, D - 2);
uf = calc_f(y, D - 2);
tmp = node(ug + uf, uf, uh, 0, ug + uf, uf);
Tag[top[x]] = Tag[top[x]] * tmp;
}
for (int i = max(0, D - ht[y] - 1); i < D && i + id[x] < ht[top[x]]; ++i) {
F[top[x]][id[x] + i] = nowf[i] - Tag[top[x]].tag_f;
G[top[x]][id[x] + i] = nowg[i] - Tag[top[x]].tag_g - Tag[top[x]].fg * F[top[x]][id[x] + i];
H[top[x]][id[x] + i] = nowh[i] - Tag[top[x]].tag_h - Tag[top[x]].kg * G[top[x]][id[x] + i] - Tag[top[x]].kf * F[top[x]][id[x] + i];
}
}
if (id[x] + D < ht[top[x]]) {
F[top[x]][id[x] + D] = -Tag[top[x]].tag_f;
G[top[x]][id[x] + D] = -Tag[top[x]].tag_g - Tag[top[x]].fg * F[top[x]][id[x] + D];
H[top[x]][id[x] + D] = -Tag[top[x]].tag_h - Tag[top[x]].kg * G[top[x]][id[x] + D] - Tag[top[x]].kf * F[top[x]][id[x] + D];
}
for (auto y : to[fa]) {
if (y == Fa) continue;
ll uf, ug, uh, vf, vg, vh;
uh = calc_h(y, DD / 2);
ug = calc_g(y, DD / 2);
uf = calc_f(y, DD / 2);
vh = calc_H(y, id[y]);
vg = calc_G(y, id[y]);
vf = calc_F(y, id[y]);
ans[y] = max(ans[y], uh + vh + ug * vf + uf * (vg + vf));
if (DD & 1) ans[fa] = max(ans[fa], uh + vh + ug * vf + uf * (vg + vf));
}
}
for (int i = 0; i < lstf[x].size(); ++i) f[top[x]][id[x] + i] = lstf[x][i];
for (int i = 0; i < lstg[x].size(); ++i) g[top[x]][id[x] + i] = lstg[x][i];
for (int i = 0; i < lsth[x].size(); ++i) h[top[x]][id[x] + i] = lsth[x][i];
tag[top[x]] = lst[x];
if (son[x]) dfs2(son[x], x, fa);
for (auto y : to[x]) {
if (y == fa || y == son[x]) continue;
dfs2(y, x, fa);
}
}
void dfs3(int x, int fa, int d) {
if (d > DD / 2) return;
fin = max(fin, ans[x]);
for (auto y : to[x])
if (y != fa) dfs3(y, x, d + 1);
}
int main() {
n = read(); DD = read(); D = (DD + 1) / 2;
for (int i = 1, x, y; i < n; ++i) {
x = read(); y = read();
to[x].push_back(y);
to[y].push_back(x);
}
dfs(1, 0, 1);
for (int i = 1; i <= n; ++i) {
if (i == top[i]) continue;
id[i] -= id[top[i]];
}
for (int i = 1; i <= n; ++i) {
if (i != top[i]) continue;
f[i].resize(ht[i]); F[i].resize(ht[i]);
g[i].resize(ht[i]); G[i].resize(ht[i]);
h[i].resize(ht[i]); H[i].resize(ht[i]);
id[i] = 0;
}
F[0].resize(n); G[0].resize(n); H[0].resize(n);
dfs1(1, 0);
if (!(DD & 1)) ans[1] = h[1][min(D, ht[1] - 1)];
dfs2(1, 0, 0);
scanf("%d", &T);
while (T--) {
int x; scanf("%d", &x);
fin = 0; dfs3(x, 0, 0);
printf("%lld\n", fin);
}
return 0;
}剪贴板
剪贴板 6mhg3q4u
剪贴板信息
剪贴板信息
- 收录
- 更新