lct 0pts 未过样例 求条
查看原帖
lct 0pts 未过样例 求条
1015770
Arteg楼主2024/5/9 09:17

调了一晚上了;
蒟蒻真的无能为力了;

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mid ((l+r)>>1)
#define ls ch[p][0]
#define rs ch[p][1]

const int maxn=200010;
const int mo=1000000007;
const int inf=0x7f7f7f7f7f7f7f7f;

int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}

int n,mm,ans[maxn],ttt;
int op[maxn],fr[maxn],to[maxn],id[maxn],tot;
int ch[maxn][2],fa[maxn],w[maxn],sum[maxn],lz[maxn],lzs[maxn],st[maxn];

unordered_map<int ,int > m;

struct E{
	int u,to,v;
}e[maxn];

void up(int p){
	sum[p]=sum[ls]+sum[rs]+w[p]; 
	return ;
}

void push(int p){
	w[p]=0;
	sum[p]=0;
	lz[p]=1;
	return ;
}

void pd(int p){
	if(lz[p]){
		//if(ls)
			push(ls);
		//if(rs)
			push(rs);
		sum[p]=0;
		lz[p]=0;
	}
	if(lzs[p]){
		swap(ls,rs);
		//if(ls)
			lzs[ls]^=1;
		//if(rs)
			lzs[rs]^=1;
		lzs[p]=0;
	}
	return ;
}

bool nrt(int p){
	return ch[fa[p]][0]==p||ch[fa[p]][1]==p;
}

void rotate(int x){
	int y=fa[x],z=fa[y],k=(ch[y][1]==x),w=ch[x][!k];
	if(nrt(y))
		ch[z][y==ch[z][1]]=x;
	ch[x][!k]=y;
	ch[y][k]=w;
	if(w)
		fa[w]=y;
	fa[x]=z;
	fa[y]=x;
	up(y);
	up(x);
	return ;
}

void splay(int x){
	int top=0,y=x,z;
	st[++top]=y;
	while(nrt(y)){
		y=fa[y];
		st[++top]=y;
	//	cout<<y<<" "<<fa[y]<<" "<<nrt(y)<<endl;
	}
	while(top)
		pd(st[top--]);
	while(nrt(x)){
		y=fa[x];	
		z=fa[y];
		if(nrt(y))
			rotate((ch[y][1]==x)^(ch[z][1]==y)?x:y);
		rotate(x);
	}
	return ;
}

void access(int x){
	for(int y=0;x;y=x,x=fa[x]){
		splay(x);
		ch[x][1]=y;
		up(x);
	}
	return ;
}

void pr(int p){
	swap(ls,rs);
	lzs[p]^=1;
	return ;
}

void mrt(int p){
	access(p);
	splay(p);
	pr(p);
	return ;
}

int findrt(int p){
	access(p);
	splay(p);
	pd(p);
	while(ch[p][0]!=0){
		p=ch[p][0];			
		pd(p);
	}	
	splay(p);
	return p;
}

void split(int x,int y){
	mrt(x);
	access(y);
	splay(y);
	return ;
}

bool link(int x,int y){
	mrt(x);
	if(findrt(y)!=x)
		fa[x]=y;
	return 1;
}

bool check(int x,int y){
	mrt(x);
	return findrt(y)==x;
}

signed main(){
	n=read();
	mm=read();
	for(int i=1;i<=mm;i++){
		e[i].u=read();
		e[i].to=read();
		if(e[i].u>e[i].to)
			swap(e[i].u,e[i].to);
		m[e[i].u*(n+1)+e[i].to]=i;
		w[i+n]=1;
	}
	while(1){
		op[++tot]=read();
		if(op[tot]==-1){
			tot--;
			break;
		}
		fr[tot]=read();
		to[tot]=read();
		if(op[tot]==0){
			if(fr[tot]>to[tot])
				swap(fr[tot],to[tot]);
			id[tot]=m[fr[tot]*(n+1)+to[tot]];
			e[id[tot]].v=1;
		}
	}
	for(int i=1;i<=mm;i++){
		if(!e[i].v){		
			int u=e[i].u,v=e[i].to;
			if(check(u,v)){
				split(u,v);
				push(v);
			}
			else{
				link(e[i].u,i+n);
				link(i+n,e[i].to);
			}
		}			
	}
	for(int i=tot;i>=1;i--){
		int u=fr[i],v=to[i];	
		if(op[i]==0){
			if(check(u,v)){
				split(u,v);
				push(v);
			}
			else{
				link(u,id[i]+n);
				link(id[i]+n,v);
			}
		}
		else{
			split(u,v);
			ans[++ttt]=sum[v];
		}
	}
	for(int i=ttt;i>=1;i--)
		printf("%lld\n",ans[i]);
	return 0;
}
2024/5/9 09:17
加载中...