根据这题条件,我们容易得出结论,一个点可以删除当且仅当这个点不在任意一个好点对构成的简单路径上,同时必须在 和 构成的简单路径上。
这个东西很好搞,上个树链剖分做个覆盖,然后拉出坏点的简单路径统计就做完了。
#define MULTI_TEST 0
int32_t main() {
#if MULTI_TEST == 1
#else
int n, a;
std::cin >> n >> a;
VVI adj(n + 1);
For(i, 1, n - 1) {
int x, y;
std::cin >> x >> y;
adj[x].eb(y), adj[y].eb(x);
}
VI pa(n + 1, 0), siz(n + 1, 0), heavy(n + 1, 0), pos(n + 1, 0);
VI dep(n + 1, 0), head(n + 1, 0);
auto dfs = [&](auto&& self, int x, int p) -> void {
pa[x] = p, dep[x] = dep[p] + 1;
siz[x] = 1;
for (auto& y : adj[x]) {
if (y == p) continue;
self(self, y, x), siz[x] += siz[y];
if (siz[y] > siz[heavy[x]]) {
heavy[x] = y;
}
}
};
int cnt = 0;
auto dfs1 = [&](auto&& self, int x, int h) -> void {
head[x] = h, pos[x] = cnt++;
if (heavy[x]) self(self, heavy[x], h);
for (auto& y : adj[x]) if (y != pa[x] && y != heavy[x])
self(self, y, y);
};
dep[0] = -1;
dfs(dfs, 1, 0), dfs1(dfs1, 1, 1);
Fenw fenw(n);
auto update = [&](int x, int y, int v) -> void {
while (head[x] != head[y]) {
if (dep[head[x]] > dep[head[y]]) {
fenw.update(pos[head[x]], v);
fenw.update(pos[x] + 1, -v);
x = pa[head[x]];
}
else {
fenw.update(pos[head[y]], v);
fenw.update(pos[y] + 1, -v);
y = pa[head[y]];
}
}
if (dep[x] > dep[y])
std::swap(x, y);
fenw.update(pos[x], v);
fenw.update(pos[y] + 1, -v);
};
auto lca = [&](int x, int y) -> int {
while (head[x] != head[y]) {
if (dep[head[x]] > dep[head[y]])
x = pa[head[x]];
else
y = pa[head[y]];
}
return dep[x] < dep[y] ? x : y;
};
auto get = [&](int x, int y) -> VI {
int l = lca(x, y);
VI px, py;
while (x != l) px.eb(x), x = pa[x]; px.eb(l);
while (y != l) py.eb(y), y = pa[y]; std::reverse(ALL(py));
for (auto& v : py) px.eb(v);
return px;
};
For(i, 0, a - 1) {
int x, y; std::cin >> x >> y;
update(x, y, 1);
}
int bu, bv;
std::cin >> bu >> bv;
auto path = get(bu, bv);
int ans = 0;
for (auto& x : path)
ans += !fenw.query(pos[x]);
std::cout << ans << "\n";
#endif
}