思路
求无向图的桥,但是卡空间,不可以直接 tarjan。
容易想到 LCT 动态维护边双连通分量。因为我们需要输出边具体是什么,所以我们把边同样化作点放入 LCT 中维护,每次加边时若两点未联通就让两点与对应边的点连边,联通就把动态树上两点之间的路径合成一个边双连通分量。因为要和并多个点,就再套一个并查集即可。
时间复杂度 。
AC code
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define f(i,j,n) for(int i=j;i<=n;i++)
#define F(i,n,j) for(int i=n;i>=j;i--)
#define updmax(a,b) a=max(a,b)
#define updmin(a,b) a=min(a,b)
#define pb push_back
using namespace std;
namespace fsd{
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin),p1==p2)?EOF:*p1++)
const int MAXSIZE=1<<20;
char buf[MAXSIZE],*p1,*p2;
inline int read(){
int ak=0,ioi=1;char c=gc();
while(!isdigit(c)){if(c=='-')ioi=-1;c=gc();}
while(isdigit(c))ak=ak*10+(c^48),c=gc();
return ak*ioi;
}
inline char readc(){
char p=gc();
while(!((p<='z'&&p>='a')||(p<='Z'&&p>='A'))){p=gc();}
return p;
}
}
using namespace fsd;
const int N=1e5+10;
int n,m;
struct LCT{
int fath[N*2];
int getfa(int k){return k==fath[k]?k:fath[k]=getfa(fath[k]);}
struct abc{
int son[2];
int fp,fq;
int tg_revs,fath;
void reverse(){
tg_revs^=1,swap(son[0],son[1]);
}
}Tree[N*2];
#define lx(k) Tree[k].son[0]
#define rx(k) Tree[k].son[1]
#define Gt(k) (Tree[Tree[k].fath].son[1]==k)
#define isroot(k) (Tree[Tree[k].fath].son[0]!=k&&Tree[Tree[k].fath].son[1]!=k)
int del[N*2],dl=0,cnt=0;
int nwid(){return dl?del[dl--]:++cnt;}
int nwnode(int fp,int fq){
int T=nwid();
fath[T]=T;
Tree[T]={0,0,fp,fq,0,0};
return T;
}
void pushdown(int k){if(Tree[k].tg_revs)Tree[lx(k)].reverse(),Tree[rx(k)].reverse(),Tree[k].tg_revs=0;}
void update(int k){
if(!isroot(k))update(Tree[k].fath=getfa(Tree[k].fath));
pushdown(k);
}
void rotate(int k){
int y=Tree[k].fath,z=Tree[y].fath,c=Gt(k);
if(!isroot(y))Tree[z].son[Gt(y)]=k;
Tree[y].son[c]=Tree[k].son[c^1],Tree[Tree[k].son[c^1]].fath=y;
Tree[k].son[c^1]=y,Tree[y].fath=k,Tree[k].fath=z;
}
void splay(int k){
update(k);
for(;!isroot(k);rotate(k)){
if(!isroot(Tree[k].fath))rotate(Gt(k)==Gt(Tree[k].fath)?Tree[k].fath:k);
}
}
void access(int k){
for(int ls=0;k;k=(Tree[k].fath=getfa(Tree[k].fath))){
splay(k),rx(k)=ls,ls=k;
}
}
void make_root(int k){
access(k),splay(k),Tree[k].reverse();
}
int find_root(int k){
access(k),splay(k),pushdown(k);
while(lx(k))k=lx(k),pushdown(k);
splay(k);
return k;
}
void PUT(int k,int T){
if(!k)return;
if(k>n)del[++dl]=k,Tree[k].fp=Tree[k].fq=0;
else fath[k]=T;
PUT(lx(k),T),PUT(rx(k),T);
}
}_lct;
void gs(){
n=read(),m=read();
f(i,1,n)_lct.nwnode(0,0);
f(i,1,m){
int l=read(),r=read();
int ls=l,rs=r;
l=_lct.getfa(l),r=_lct.getfa(r);
if(_lct.make_root(l),_lct.find_root(r)!=l){
int T=_lct.nwnode(ls,rs);
_lct.Tree[l].fath=T,_lct.Tree[T].fath=r;
}else{
_lct.PUT(l,l);
_lct.Tree[l].son[0]=_lct.Tree[l].son[1]=0;
}
}
f(i,1,_lct.cnt)if(_lct.Tree[i].fp)printf("%lld %lld\n",_lct.Tree[i].fp,_lct.Tree[i].fq);
}
signed main(){
// freopen("p_jx.out","r",stdin);
// freopen("pipe.out","w",stdout);
#ifndef XQZ
freopen("pipe.in","r",stdin);
freopen("pipe.out","w",stdout);
#endif
#ifdef NXD
int t=0;cin>>t;while(t--)
#endif
gs();
return 0;
}
//sample ok
//big data ok