P4320 的新奇解法
NaCly_Fish · · 题解
这题怎么能没有我大LCT的题解呢?
虽然有人发过一篇,但还是用的tarjan算法,并没有完全地使用LCT。
这里给出我的解法。
按照一般的做法,需要构造圆方树。
那这个东西我们也可以用LCT来维护。
一开始图上有
若有一条边要连上
- 若未连通,直接将
u,v 两点连上。 - 若已经连通,则会复杂一些:需要把
u 到v 上的所有边断掉,然后再把路径上的所有点都连向一个方点(即0 权点)。
好了那怎么找出这条路径呢?
按照一般路径查询的套路,我们使用
这个时候,路径上的所有点都在这棵
不过遍历时要注意一点,需要先
具体代码实现是这个样子的:
void dfs(int u){
pushdown(u);
if(son[u][1]) dfs(son[u][1]);
stack[++cnt] = u; //最后这个stack里面存的就是路径上的点
if(son[u][0]) dfs(son[u][0]);
}
inline void find_path(int u,int v){
split(u,v);
cnt = 0;
dfs(v);
}
剩下就没什么好说的了,直接上个LCT板子就过啦
ps:常数巨大
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define N 2000007
#define reg register
#define ls son[u][0]
#define rs son[u][1]
using namespace std;
int uf[N];
int n,m,q,img,top;
inline void read(int &x);
void print(int x);
inline int find(int x);
struct Link_Cut_Tree{
int s[N],st[N],r[N];
int fa[N],son[N][2],a[N],st2[N];
inline bool notroot(int u){
return son[fa[u]][0]==u||son[fa[u]][1]==u;
}
inline void pushup(int u){
s[u] = s[ls]+s[rs]+a[u];
}
inline void pushr(int u){
swap(ls,rs);
r[u] ^= 1;
}
inline void pushdown(int u){
if(!r[u]) return;
if(ls) pushr(ls);
if(rs) pushr(rs);
r[u] = 0;
}
inline void rotate(int x){
int y = fa[x],z = fa[y];
int k = son[y][1]==x,w = son[x][k^1];
if(notroot(y)) son[z][son[z][1]==y] = x;
son[x][k^1] = y;
son[y][k] = w;
if(w) fa[w] = y;
fa[y] = x,fa[x] = z;
pushup(y);
}
inline void splay(int x){
int y = x,z = 0;
st[++z] = y;
while(notroot(y)) st[++z] = y = fa[y];
while(z) pushdown(st[z--]);
while(notroot(x)){
y = fa[x],z = fa[y];
if(notroot(y)){
if((son[z][1]==y)==(son[y][1]==x)) rotate(y);
else rotate(x);
}
rotate(x);
}
pushup(x);
}
inline void access(int u){
for(reg int y=0;u;u=fa[u]){
splay(u);
rs = y;
pushup(u);
y = u;
}
}
inline void makeroot(int u){
access(u),splay(u);
pushr(u);
}
inline int findroot(int u){
access(u),splay(u);
while(ls){
pushdown(u);
u = ls;
}
splay(u);
return u;
}
inline void split(int u,int v){
makeroot(u);
access(v),splay(v);
}
inline void link(int u,int v){
makeroot(u);
fa[u] = v;
}
inline void cut(int u,int v){
makeroot(u);
if(findroot(v)!=u||fa[v]!=u||son[v][0]) return;
fa[v] = son[u][1] = 0;
pushup(u);
}
inline int query(int u,int v){
split(u,v);
return s[v];
}
void divide(int u){
pushdown(u);
if(rs) divide(rs);
st2[++top] = u;
if(ls) divide(ls);
}
inline void rebuild(int u,int v,int w){
split(u,v);
top = 0;
divide(v);
for(reg int i=1;i<top;++i) cut(st2[i],st2[i+1]); //把原先路径上的边都断掉
for(reg int i=1;i<=top;++i) link(st2[i],w); //连到一个方点上去
}
}T;
int main(){
int u,v;
read(n),read(m);
for(reg int i=1;i<=n;++i){
uf[i] = i;
T.a[i] = 1; //初始的n个点权值都是1
}
img = n+1;
for(reg int i=1;i<=m;++i){
read(u),read(v);
if(find(u)==find(v)){
if(T.query(u,v)<=2) continue; //剪枝优化: 如果除了端点,剩下的都是方点,就可以直接跳过,无需操作
T.rebuild(u,v,img++);
}else{
T.link(u,v);
uf[find(u)] = find(v);
}
}
read(q);
while(q--){
read(u),read(v);
print(T.query(u,v));
putchar('\n');
}
return 0;
}
inline int find(int x){
while(x^uf[x]) x = uf[x] = uf[uf[x]];
return x;
}
inline void read(int &x){
x = 0;
char c = getchar();
while(c<'0'||c>'9') c = getchar();
while(c>='0'&&c<='9'){
x = (x<<3)+(x<<1)+(c^48);
c = getchar();
}
}
void print(int x){
if(x>9) print(x/10);
putchar(x%10+'0');
}
ps ps:关于复杂度的势能分析(不严谨)证明
设这棵树一开始的势能为
每次进行
但与此同时,又有一条长度为
这样就可以看做是它的势能降低了
我们发现对于
于是我们的复杂度就是