zoukankan      html  css  js  c++  java
  • [QTree6]Query on a tree VI

    Description:

    给你一棵n个点的树,编号1~n。每个点可以是黑色,可以是白色。初始时所有点都是黑色。下面有两种操作请你操作给我们看:

    0 u:询问有多少个节点v满足路径u到v上所有节点(包括)都拥有相同的颜色
    1 u:翻转u的颜色

    Hint:

    (nle 10^5)

    Solution:

    这题我一开始用树剖写,然后随机数据跑得飞快,交上去被菊花图卡飞23333333

    树剖正解,详见https://www.cnblogs.com/ivorysi/p/10103010.html

    但是.......树剖写法太毒瘤了!!!

    所以这里介绍的是LCT做法

    不得不说比较巧妙

    考虑用2颗LCT维护两种颜色的联通块

    并且把点的颜色存到边上

    每次修改就在一颗LCT上断边,另一颗LCT上连这条边

    同时LCT维护子树信息,询问直接搞就行了

    我的LCT还是太菜了,看了好久才看懂

    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #define ls p<<1 
    #define rs p<<1|1
    using namespace std;
    typedef long long ll;
    const int mxn=1e6+5;
    int n,m,cnt=1;
    int f[mxn],hd[mxn],col[mxn];
    
    inline int read() {
        char c=getchar(); int x=0,f=1;
        while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
        while(c<='9'&&c>='0') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
        return x*f;
    }
    inline void chkmax(int &x,int y) {if(x<y) x=y;}
    inline void chkmin(int &x,int y) {if(x>y) x=y;}
    
    struct ed {
        int to,nxt;
    }t[mxn<<1];
    
    inline void add(int u,int v) {
        t[++cnt]=(ed) {v,hd[u]}; hd[u]=cnt;
    }
    
    struct LCT {
        int fa[mxn],s[mxn],sz[mxn],ch[mxn][2];
        void push_up(int x) {
            sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+s[x]+1;
        }
        int isnotrt(int x) {
            return ch[fa[x]][0]==x||ch[fa[x]][1]==x;
        }
        void rotate(int x) {
            int y=fa[x],z=fa[y],tp=ch[y][1]==x;
            if(isnotrt(y)) ch[z][ch[z][1]==y]=x; fa[x]=z;
            ch[y][tp]=ch[x][tp^1]; fa[ch[x][tp^1]]=y;
            ch[x][tp^1]=y; fa[y]=x;
            push_up(y),push_up(x);
        }
        void splay(int x) {
            while(isnotrt(x)) {
                int y=fa[x],z=fa[y];
                if(isnotrt(y))
                    (ch[y][1]==x)^(ch[z][1]==y)?rotate(x):rotate(y);
                rotate(x);  
            }
        }
        void access(int x) {
            for(int y=0;x;x=fa[y=x]) {
                splay(x); 
                s[x]+=sz[ch[x][1]];
                ch[x][1]=y;
                s[x]-=sz[ch[x][1]];
            }
        }
        int findrt(int x) {
            access(x); splay(x);
            while(ch[x][0]) x=ch[x][0];
            splay(x); return x;
        }
        void link(int x) {
            splay(x); fa[x]=f[x];
            int y=f[x]; access(y); splay(y);
            s[y]+=sz[x]; sz[y]+=sz[x];
        }
        void cut(int x) {
            access(x); splay(x);
            ch[x][0]=fa[ch[x][0]]=0;
            push_up(x);
        }
    }lct[2];
    
    void dfs(int u,int fa) {
        for(int i=hd[u];i;i=t[i].nxt) {
            int v=t[i].to;
            if(v==fa) continue ;
            dfs(v,u); f[v]=u; lct[0].link(v);
        }
    }
    
    int main()
    {
        n=read(); int u,v;
        for(int i=1;i<=n+1;++i) lct[0].sz[i]=lct[1].sz[i]=1; //千万不要忘记赋初值
        for(int i=1;i<n;++i) {
            u=read(); v=read();
            add(u,v); add(v,u);
        }
        dfs(1,0); f[1]=n+1; //1节点也必须有父亲
        lct[0].link(1); m=read();
        for(int i=1;i<=m;++i) {
            u=read(); v=read();
            if(u==1) lct[col[v]].cut(v),lct[col[v]^=1].link(v);
            else {
                int tp=lct[col[v]].findrt(v);
                printf("%d
    ",lct[col[v]].sz[lct[col[v]].ch[tp][1]]);
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    对于有API接口数据的监测
    网站、域名、IP、URL、URI、IRI
    面向对象总结
    封装示例:游戏开发
    面向对象基础2
    继承与多态
    如何引用第三方同级以及不同级目录下的的py文件
    Weblogic常见报错以及解决方法
    深度解读企业云上办公利器「无影云电脑」
    java根据模板导出pdf
  • 原文地址:https://www.cnblogs.com/list1/p/10485883.html
Copyright © 2011-2022 走看看