zoukankan      html  css  js  c++  java
  • 洛谷P3950 部落冲突(LCT)

    洛谷题目传送门

    最无脑LCT题解,Dalao们的各种算法都比这个好多啦。。。

    唯一的好处就是只管码代码就好了

    开战cut,停战link,询问findroot判连通性

    太无脑,应该不用打注释了。常数大就不用说了(逃

    #include<cstdio>
    #include<cstdlib>
    #define R register int
    #define I inline void
    #define lc c[x][0]
    #define rc c[x][1]
    #define G ch=getchar()
    #define gc G;while(ch<'-')G
    #define in(z) gc;z=ch&15;G;while(ch>'-')z*=10,z+=ch&15,G;
    const int N=300009;
    int f[N],c[N][2],st[N],u[N],v[N];
    bool r[N];
    inline bool nroot(R x){
    	return c[f[x]][0]==x||c[f[x]][1]==x;
    }
    I pushdown(R x){
    	if(r[x]){
    		R t=lc;lc=rc;rc=t;
    		r[lc]^=1,r[rc]^=1,r[x]=0;
    	}
    }
    I rotate(R x){
    	R y=f[x],z=f[y],k=c[y][1]==x,w=c[x][!k];
    	if(nroot(y))c[z][c[z][1]==y]=x;c[x][!k]=y;c[y][k]=w;
    	if(w)f[w]=y;f[y]=x;f[x]=z;
    }
    I splay(R x){
    	R 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];z=f[y];
    		if(nroot(y))
    			rotate((c[y][0]==x)^(c[z][0]==y)?y:x);
    		rotate(x);
    	}
    }
    I access(R x){
    	for(R y=0;x;x=f[y=x])
    		splay(x),rc=y;
    }
    I makeroot(R x){
    	access(x);splay(x);
    	r[x]^=1;
    }
    inline int findroot(R x){
    	access(x);splay(x);
        pushdown(x);
    	while(lc)pushdown(x=lc);
    	return x;
    }
    I split(R x,R y){
    	makeroot(x);
    	access(y);splay(y);
    }
    I link(R x,R y){
    	makeroot(x);f[x]=y;
    }
    I cut(R x,R y){
    	split(x,y);f[x]=c[y][0]=0;
    }
    int main()
    {
    	register char ch;
    	R n,m,p=0,a,b;
    	in(n);in(m);
    	for(R i=1;i<n;++i){in(a);in(b);link(a,b);}
    	while(m--){
    		gc;
    		switch(ch){
    		case 'U':in(a);link(u[a],v[a]);break;
    		case 'C':in(a);in(b);++p;cut(u[p]=a,v[p]=b);break;
    		case 'Q':in(a);in(b);puts(findroot(a)==findroot(b)?"Yes":"No");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    YUI+Ant 实现JS CSS压缩
    13.QT-QMainWindow组件使用
    12.QT4.7.4-解决WIN平台和Linux平台中文乱码,QLineEdit右击菜单中文显示
    11.QT-布局管理器(Box,Grid,Form,Stacked)
    10.QT-定时器
    9.QT-标准对话框
    8.QT-对话框(模态与非模态)
    7.QT-Qt对象间的父子关系
    6.QT-简易计算器实现(详解)
    5.QT-QString类
  • 原文地址:https://www.cnblogs.com/flashhu/p/8653506.html
Copyright © 2011-2022 走看看