题解 P4299 首都
一个 LCT + 二分查找重心的做法。
题意
给定一个森林,支持以下操作:
A x y:连接x,y 。Q x:查询点x 所在树的重心。Xor:查询所有树的重心编号的异或和。
简化版题意:动态维护树重心。
题解
对于节点
对于连边操作,直接调用 LCT 的
维护重心需要两个性质:
-
以重心为根的最大的子树节点数小于总节点数的一半。
-
连接两棵树后,新的重心必定在两棵树原重心的路径上。
两个性质都可用反证法证,令
对于性质 1,若
对于性质 2,设点
如图,两棵树重心分别是
连接
为了方便处理,用重心来代表一棵树,再用并查集维护,每个点的并查集存所在树的重心,连边后就把原来两棵树重心
需要注意,当树的大小为奇数时,只有一个重心,树的大小为偶数时可能会有多个重心,要找序号最小的。
具体细节看代码叭。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct Link_Cut_Tree {
struct node {
int fa, ch[2], siz, tot;
bool tag;
} t[N];
bool nroot(int x) { return t[t[x].fa].ch[0] == x || t[t[x].fa].ch[1] == x; }
void pushup(int x) { t[x].siz = t[t[x].ch[0]].siz + t[t[x].ch[1]].siz + 1 + t[x].tot; }
void pushr(int x) {
swap(t[x].ch[0], t[x].ch[1]);
t[x].tag ^= 1;
}
void pushdown(int x) {
if(!t[x].tag) return;
if(t[x].ch[0]) pushr(t[x].ch[0]);
if(t[x].ch[1]) pushr(t[x].ch[1]);
t[x].tag = 0;
}
void pushdown_path(int x) {
if(nroot(x))
pushdown_path(t[x].fa);
pushdown(x);
}
void rotate(int x) {
int y = t[x].fa, z = t[y].fa, ych = t[y].ch[1] == x, w = t[x].ch[!ych];
if(nroot(y)) t[z].ch[t[z].ch[1] == y] = x;
t[x].ch[!ych] = y, t[y].ch[ych] = w;
if(w) t[w].fa = y;
t[y].fa = x, t[x].fa = z;
pushup(y);
}
void splay(int x) {
pushdown_path(x);
int y;
while(nroot(x)) {
y = t[x].fa;
if(nroot(y))
rotate(t[y].ch[1] == x ^ t[t[y].fa].ch[1] == y ? x : y);
rotate(x);
}
pushup(x);
}
void access(int x) {
for(int y = 0; x; x = t[y = x].fa)
splay(x), t[x].tot += t[t[x].ch[1]].siz - t[y].siz, t[x].ch[1] = y, pushup(x);
}
void makeroot(int x) {
access(x);
splay(x);
pushr(x);
}
int findroot(int x) {
access(x);
splay(x);
while(t[x].ch[0]) pushdown(x), x = t[x].ch[0];
splay(x);
return x;
}
void split(int x, int y) {
makeroot(x);
access(y);
splay(y);
}
void link(int x, int y) {
split(x, y);
t[t[x].fa = y].tot += t[x].siz;
pushup(y);
}
int dfs(int x) {
int lsum = 0, rsum = 0, sum = t[x].siz >> 1, bre = t[x].siz & 1, u = 1e9, curl, curr;
for(;x;) {
pushdown(x);
curl = lsum + t[t[x].ch[0]].siz;
curr = rsum + t[t[x].ch[1]].siz;
if(curl <= sum && curr <= sum) {
if(bre) {
u = x;
break;
}
u = min(u, x);
}
if(curl < curr) {
lsum += t[t[x].ch[0]].siz + t[x].tot + 1;
x = t[x].ch[1];
}
else {
rsum += t[t[x].ch[1]].siz + t[x].tot + 1;
x = t[x].ch[0];
}
}
splay(u);
return u;
}
} LCT;
int n, m;
int fa[N], ans;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
fa[i] = i, ans ^= i;
char op[3];
int x, y;
while(m--) {
scanf("%s", op);
if(op[0] == 'A') {
scanf("%d%d", &x, &y);
LCT.link(x, y);
x = find(x), y = find(y);
LCT.split(x, y);
int g = LCT.dfs(y);
ans ^= x ^ y ^ g;
fa[x] = fa[y] = fa[g] = g;
}
else if(op[0] == 'Q') {
scanf("%d", &x);
printf("%d\n", find(x));
}
else printf("%d\n", ans);
}
return 0;
}
这道题挺难理解的,最好是自己可以画画图。