题解:AT_abc460_f [ABC460F] Farthest Pair Query
lzm2025
·
·
题解
依旧赛时没调出来,再次急哭。
题意简述
给定一颗 n 个点的树,初始每个结点都是黑色。执行 q 次操作,每次翻转一个点的颜色,然后输出任意两个黑色点之间的距离的最大值。
思路
题目相当于求黑点构成的子图的直径。对于直径有经典结论:若树上子图 S 和 T 的一条直径分别为 (a,b) 和 (c,d),则 S \cup T 的直径一定能从 \{a,b,c,d\} 中选出。
::::info[证明]{open}
令 \operatorname{d}(u,v) 表示树上 u 和 v 的距离。
先证明引理:若树上 S 的一条直径为 (a,b)。给定一个树上结点 u,则 \max_{v \in S} \operatorname{d}(u,v) = \max\{\operatorname{d}(u,a),\operatorname{d}(u,b)\}。
:::info[引理的证明]{open}

只需证明 $\operatorname{d}(u,v) \le \operatorname{d}(u,b)$。
根据直径的性质有 $\operatorname{d}(a,v) \le \operatorname{d}(a,b)$,两边同时减去 $\operatorname{d}(a,p_v)$ 得到 $\operatorname{d}(p_v,v) \le \operatorname{d}(p_v,b)$,两边同时加上 $\operatorname{d}(u,p_v)$ 得到 $\operatorname{d}(u,v) \le \operatorname{d}(u,b)$。证毕。
:::
$\forall u,v \in S \cup T$,只需证明 $\operatorname{d}(u,v) \le \max_{x,y \in \{a,b,c,d\}} \operatorname{d}(x,y)$。
- 若 $u,v \in S$(或 $T$),则 $\operatorname{d}(u,v) \le \operatorname{d}(a,b)$(或 $\operatorname{d}(c,d)$),上式显然成立。
- 否则,设 $u \in S$,$v \in T$,根据引理有:
$$\begin{aligned} \operatorname{d}(u,v) &\le \max\{\operatorname{d}(u,c),\operatorname{d}(u,d)\} \\ &\le \max\{\max\{\operatorname{d}(a,c),\operatorname{d}(b,c)\},\max\{\operatorname{d}(a,d),\operatorname{d}(b,d)\}\} \end{aligned}$$
综上所述,该结论成立。
::::
我们考虑使用线段树优化修改。
初始时按结点编号 $1 \sim n$ 建树,结点 $[l,r]$ 存储编号在 $[l,r]$ 之间的结点构成的集合的直径端点和长度。由于初始全黑,对于叶子结点 $u$ 设为直径 $(u,u)$。
修改时,对于叶子结点 $u$,若修改为白点,则将直径设为不合法值(我代码里取了 $(0,0)$);否则将直径设为 $(u,u)$。合并时取出两个叶子节点的直径端点并去除不合法值得到集合 $S$。根据上述结论,我们只需枚举 $S$ 中任意两个元素并求出 $(x,y)(x,y \in S)$ 使 $\operatorname{d}(x,y)$ 最大,并使该节点存储直径 $(x,y)$。
若使用倍增求 LCA,时间复杂度为 $O((n+q) \log^2 n)$,常数较大,但足以通过本题。
## 代码
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, q;
vector<int> g[100007];
int dep[100007];
int f[100007][18];
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1;
for (int i = 1; i <= 17; i++)
f[u][i] = f[f[u][i - 1]][i - 1];
for (auto v : g[u]) {
if (v == fa)
continue;
f[v][0] = u;
dfs(v, u);
}
}
inline int lca(int x, int y) {
if (dep[x] < dep[y])
swap(x, y);
for (int i = 17; i >= 0; i--)
if (dep[f[x][i]] >= dep[y])
x = f[x][i];
if (x == y)
return x;
for (int i = 17; i >= 0; i--)
if (f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
return f[x][0];
}
inline int dis(int x, int y) { return dep[x] + dep[y] - (dep[lca(x, y)] << 1); }
struct node {
int u, v, d; // 直径节点,长度
node(int _u = 0, int _v = 0, int _d = -1) : u(_u), v(_v), d(_d) {}
inline node operator+(const node &oth) const { // 合并
node ans;
if (d > oth.d) // 小优化:算过的不重复计算
ans = *this;
else
ans = oth;
int tmp;
if (u && oth.u && (tmp = dis(u, oth.u)) > ans.d) { // 枚举
ans.u = u;
ans.v = oth.u;
ans.d = tmp;
}
if (u && oth.v && (tmp = dis(u, oth.v)) > ans.d) {
ans.u = u;
ans.v = oth.v;
ans.d = tmp;
}
if (v && oth.u && (tmp = dis(v, oth.u)) > ans.d) {
ans.u = v;
ans.v = oth.u;
ans.d = tmp;
}
if (v && oth.v && (tmp = dis(v, oth.v)) > ans.d) {
ans.u = v;
ans.v = oth.v;
ans.d = tmp;
}
return ans;
}
};
struct SegTree {
node tree[262149];
#define ls (p << 1)
#define rs ((p << 1) | 1)
inline void push_up(int p) { tree[p] = tree[ls] + tree[rs]; }
void update(int pos, bool d, int l, int r, int p) {
if (l == r) {
if (d)
tree[p] = node(pos, pos, 0);
else
tree[p] = node();
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
update(pos, d, l, mid, ls);
else
update(pos, d, mid + 1, r, rs);
push_up(p);
}
#undef ls
#undef rs
} t;
bitset<100007> st;
int x;
signed main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1, 0);
st.set();
for (int i = 1; i <= n; i++)
t.update(i, true, 1, n, 1);
cin >> q;
while (q--) {
cin >> x;
st[x] = !st[x];
t.update(x, st[x], 1, n, 1);
cout << t.tree[1].d << '\n';
}
}