zoukankan      html  css  js  c++  java
  • hdu 2475 动态树

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int maxn = 50050;
    int n;
    struct Link_cut_tree {
        int bef[maxn],pre[maxn],next[maxn][2];
        void init() {
            memset(pre,0,sizeof(pre));
            memset(next,0,sizeof(next));
        }
        inline void rotate(int x,int kind) {
            int y,z;
            y = pre[x];
            z = pre[y];
            next[y][!kind] = next[x][kind];
            pre[next[x][kind]] = y;
            next[z][next[z][1]==y] = x;
            pre[x] = z;
            next[x][kind] = y;
            pre[y] = x;
        }
        void splay(int x) {
            int rt;
            for(rt=x;pre[rt];rt=pre[rt]);
            if(x != rt) {
                bef[x] = bef[rt];
                bef[rt] = 0;
                while(pre[x]) {
                    if(next[pre[x]][0] == x)
                        rotate(x,1);
                    else
                        rotate(x,0);
                }
            }
        }
        void access(int x) {
            int father;
            for(father=0;x;x=bef[x]) {
                splay(x);
                pre[next[x][1]] = 0;
                bef[next[x][1]] = x;
                next[x][1] = father;
                pre[father] = x;
                bef[father] = 0;
                father = x;
            }
        }
        int query(int x) {
            access(x);
            splay(x);
            while(next[x][0]) x = next[x][0];
            return x;
        }
        void cut(int x) {
            access(x);
            splay(x);
            bef[next[x][0]] = bef[x];
            bef[x] = 0;
            pre[next[x][0]] = 0;
            next[x][0] = 0;
        }
        void join(int x,int y) {
            if(y == 0) cut(x);
            else {
                int tmp;
                access(y);
                splay(y);
                for(tmp=x;pre[tmp];tmp=pre[tmp]);
                if(tmp != y) {
                    cut(x);
                    bef[x] = y;
                }
            }
        }
    } lct;
    void scanf_(int &num){
        char in;
        while((in=getchar())>'9'||in<'0');
        num=in-'0';
        while(in=getchar(),in>='0'&&in<='9')
            num*=10,num+=in-'0';
    }
    void scanf__(char &in){
        while((in=getchar())>'Z'||in<'A');
    }
    char ch;
    int x , y;
    int main() {
        bool flag = false;
        while(~scanf("%d",&n)) {
            if(!flag) flag = true;
            else puts("");
            lct.init();
            for(int i=1;i<=n;i++) scanf_(lct.bef[i]);
            int m; scanf_(m);
            while(m--) {
                scanf__(ch);
                if(ch == 'Q') {
                    scanf_(x);
                    printf("%d
    ",lct.query(x));
                }
                else {
                    scanf_(x);scanf_(y);
                    lct.join(x,y);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    windows 创建python独立开发环境
    sql多列排序
    mysql 导入sql脚本中文乱码问题
    廖雪峰Python教学课后作业---datetime
    poj 1004:Financial Management(水题,求平均数)
    【POJ水题完成表】
    poj 1003:Hangover(水题,数学模拟)
    ytu 2558: 游起来吧!超妹!(水题,趣味数学题)
    poj 1005:I Think I Need a Houseboat(水题,模拟)
    hdu 2393:Higher Math(计算几何,水题)
  • 原文地址:https://www.cnblogs.com/tobec/p/3398293.html
Copyright © 2011-2022 走看看