zoukankan      html  css  js  c++  java
  • cogs1889 [SDOI2008]Cave 洞穴勘测 link-cut tree

    link-cut tree

    // It is made by XZZ
    #include<cstdio>
    #include<algorithm>
    #define il inline
    #define rg register
    #define vd void
    #define sta static
    typedef long long ll;
    il int gi(){
        rg int x=0,f=1;rg char ch=getchar();
        while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
        while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
        return x*f;
    }
    const int maxn=10001;
    namespace LCT{
    	int ch[maxn][2],fa[maxn];bool rev[maxn];
    	il vd down(int x){if(rev[x])rev[x]=0,rev[ch[x][0]]^=1,rev[ch[x][1]]^=1,std::swap(ch[x][0],ch[x][1]);}
    	il bool isrt(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
    	il vd rot(int x){
    		sta int y,z,o;
    		y=fa[x],z=fa[y],o=x==ch[y][1];
    		if(!isrt(y))ch[z][y==ch[z][1]]=x;fa[x]=z;
    		ch[y][o]=ch[x][!o];fa[ch[x][!o]]=y;
    		ch[x][!o]=y;fa[y]=x;
    	}
    	il vd splay(int x){
    		sta int stk[maxn],top;stk[top=1]=x;
    		for(rg int i=x;!isrt(i);i=fa[i])stk[++top]=fa[i];
    		while(top)down(stk[top--]);
    		for(rg int y=fa[x],z=fa[y];!isrt(x);rot(x),y=fa[x],z=fa[y])
    			if(!isrt(y))rot(((x==ch[y][1])^(y==ch[z][1]))?y:x);
    	}
    	il vd access(int x){for(rg int y=0;x;x=fa[y=x])splay(x),ch[x][1]=y;}
    	il vd makert(int x){access(x),splay(x);rev[x]^=1;}
    	il int find(int x){for(access(x),splay(x);ch[x][0];x=ch[x][0]);return x;}
    	il vd link(int x,int y){makert(x),fa[x]=y;}
    	il vd cut(int x,int y){makert(x);access(y),splay(y);fa[x]=ch[y][0]=0;}
    }
    int main(){
     	freopen("sdoi2008_cave.in","r",stdin);
     	freopen("sdoi2008_cave.out","w",stdout);
    	using namespace LCT;
    	int n,m,u,v;char opt[20],str[][5]={"No","Yes"};
    	n=gi(),m=gi();
    	while(m--){
    		scanf("%s",opt);u=gi(),v=gi();
    		if(opt[0]=='Q')puts(str[find(u)==find(v)]);
    		else if(opt[0]=='C')link(u,v);
    		else cut(u,v);
    	}
        return 0;
    }
    

    常数有点小大。

  • 相关阅读:
    JavaScript 正则表达式(RegExp)
    impala 时间格式转换
    Informatica TO_BIGINT,TO_DECIMAL 转 字符串
    Hive metastore三种配置方式
    Windows10 从零搭建 Hadoop/Hive 环境及 Hive 入门
    Window 安装Hive
    使用dynamic-datasource-spring-boot-starter配置多数据源
    Sqoop 安装与简单测试
    使用dynamic-datasource-spring-boot-starter做多数据源及源码分析
    真正了解sqoop的一切
  • 原文地址:https://www.cnblogs.com/xzz_233/p/cogs1889.html
Copyright © 2011-2022 走看看