LCT20分求助
查看原帖
LCT20分求助
285617
黑影洞人楼主2022/9/13 16:54
#include<cstdio>
#include<algorithm>
#define N 1919810
using namespace std;
int n,m;
struct link_cut_tree{
	int ch[N][2],f[N],st[N];
	bool r[N],mark[N],go[N];
	#define lc ch[x][0]
	#define rc ch[x][1]
	void pushup(int x){go[x]=go[lc]|go[rc]|mark[x];}
	int son(int x){return x==ch[f[x]][1];}
	int nroot(int x){return x==ch[f[x]][1]||x==ch[f[x]][0];}
	void rev(int x){swap(lc,rc);r[x]^=1;}
	void rotate(int x){
		int y=f[x],z=f[y],k=son(x),w=ch[x][!k];
		if(nroot(y))ch[z][son(y)]=x;
		ch[x][!k]=y;ch[y][k]=w;
		if(w)f[w]=y;
		f[x]=z,f[y]=x;
		pushup(y);
	}
	void pushdown(int x){
		if(r[x]){
			if(lc)rev(lc);
			if(rc)rev(rc);
			r[x]=0;
		}
	}
	void splay(int x){
		int y=x,z=0;
		st[++z]=y;
		while(nroot(y))st[++z]=y=f[y];
		while(z)pushdown(st[z--]);
		while(nroot(x)){
			y=f[x];
			if(nroot(y))rotate(son(x)!=son(y)?x:y);
			rotate(x);
		} 
		pushup(x);
	}
	void access(int x){for(int y=0;x;x=f[y=x])splay(x),rc=y,pushup(x);}
	void makeroot(int x){access(x),splay(x),rev(x);}
	void split(int x,int y){makeroot(x),access(y),splay(y);}
	void link(int x,int y){makeroot(x),f[x]=y;}
	void markp(int p){makeroot(p);mark[p]=1,pushup(p);}
	int bs(int x){
		if(rc&&go[rc])return bs(rc);
		if(mark[x])return x;
		if(lc&&go[lc])return bs(lc);
		return 1;
	} 
	int query(int x){
		makeroot(x);
		return bs(x);
	}
}lct;
signed main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		lct.link(a,b);
	}
	lct.mark[1]=1;
	lct.makeroot(1);
	while(m--){
		char s[12];
		int a;
		scanf("%s%d",s,&a);
		if(s[0]=='Q')printf("%d\n",lct.query(a));
		else lct.markp(a);
	}
	return 0;
}



2022/9/13 16:54
加载中...