zoukankan      html  css  js  c++  java
  • 部落冲突

    题目链接

    戳我

    (Solution)

    不详细讲了。说一下大致过程,毕竟很裸

    对于开战:(cut(p,q))

    战争结束:(link(p[x],q[x]))

    询问:用(findroot(p)==findroot(q))判一下联通就好了

    (Code)

    #include<bits/stdc++.h>
    #define rg register
    #define file(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
    using namespace std;
    int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') f=(c=='-')?-1:1,c=getchar();
        while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();
        return f*x;
    }
    struct node {
        int fa,v,lazy,ch[2];
    }a[1000010];
    stack<int>s;
    bool nroot(int x){
        return a[a[x].fa].ch[0]==x||a[a[x].fa].ch[1]==x;
    }
    void work(int x){
        swap(a[x].ch[0],a[x].ch[1]),a[x].lazy^=1;
    }
    void pushdown(int x){
        if(a[x].lazy){
        	if(a[x].ch[0]) work(a[x].ch[0]);
        	if(a[x].ch[1]) work(a[x].ch[1]);
        	a[x].lazy=0;
        }
    }
    void rotate(int x){
        int y=a[x].fa,z=a[y].fa,k=(a[y].ch[1]==x);
        if(nroot(y)) a[z].ch[a[z].ch[1]==y]=x;
        a[x].fa=z,a[a[x].ch[k^1]].fa=y;
        a[y].ch[k]=a[x].ch[k^1],a[y].fa=x;
        a[x].ch[k^1]=y;
    }
    void splay(int x){
        int u=x;
        s.push(u);
        while(nroot(u)) s.push(u=a[u].fa);
        while(!s.empty()) pushdown(s.top()),s.pop();
        while(nroot(x)){
        	int y=a[x].fa,z=a[y].fa;
        	if(nroot(y))
        	    (a[y].ch[0]==x)^(a[z].ch[0]==y)?rotate(x):rotate(y);
        	rotate(x);
        }
    }
    void access(int x){
        for(int y=0;x;y=x,x=a[x].fa)
            splay(x),a[x].ch[1]=y;
    }
    void makeroot(int x){
        access(x),splay(x),work(x);
    }
    int findroot(int x){
        access(x),splay(x);
        while(a[x].ch[0]) pushdown(x),x=a[x].ch[0];
        splay(x);
        return x;
    }
    void split(int x,int y){
        makeroot(x),access(y),splay(y);
    }
    void link(int x,int y){
        makeroot(x);
        if(findroot(y)!=x)
        	a[x].fa=y;
    }
    void cut(int x,int y){
        makeroot(x);
        if(findroot(y)!=x||a[y].fa!=x||a[y].ch[0]) return ;
        a[y].fa=a[x].ch[1]=0;
    }
    bool pd(int x,int y){
        return findroot(y)==findroot(x);
    }
    char c[10];
    int b[1000001][2];
    int main(){
        int n=read(),m=read(),p,q,tot=0;
        for(int i=1;i<n;i++)
        	p=read(),q=read(),link(p,q);
        for(int i=1;i<=m;i++){
        	scanf("%s",c);
        	if(c[0]=='C')
        	    p=read(),q=read(),cut(q,p),b[++tot][0]=p,b[tot][1]=q;
        	if(c[0]=='U')
        	    p=read(),link(b[p][0],b[p][1]);
           	if(c[0]=='Q')
        	    p=read(),q=read(),puts(pd(p,q)?"Yes":"No");
        }
        return 0;
    }
    
    
  • 相关阅读:
    css3中强大的filter(滤镜)属性
    traceroute/tracert--获取网络路由路径
    CSS3 clip-path & clip-path 打破矩形设计的限制
    你未必知道的49个CSS知识点
    CentOS7使用firewalld打开关闭防火墙与端口
    部署K8S集群
    python 二分查找代码
    python进程间的通信
    一个线程1让线程2去调用一个函数怎么实现
    设计模式之工厂模式代码
  • 原文地址:https://www.cnblogs.com/hbxblog/p/10651027.html
Copyright © 2011-2022 走看看