zoukankan      html  css  js  c++  java
  • BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)

    Time Limit: 10 Sec  Memory Limit: 64 MB
    Submit: 587  Solved: 259
    [Submit][Status][Discuss]

    Description

    对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由千百万星球构成的Samuel星系。 星际空间站的Samuel II巨型计算机经过长期探测,已经锁定了Samuel星系中许多星球的空间坐标,并对这些星球从1开始编号1、2、3……。 一些先遣飞船已经出发,在星球之间开辟探险航线。 探险航线是双向的,例如从1号星球到3号星球开辟探险航线,那么从3号星球到1号星球也可以使用这条航线。 例如下图所示:  在5个星球之间,有5条探险航线。 A、B两星球之间,如果某条航线不存在,就无法从A星球抵达B星球,我们则称这条航线为关键航线。 显然上图中,1号与5号星球之间的关键航线有1条:即为4-5航线。 然而,在宇宙中一些未知的磁暴和行星的冲撞,使得已有的某些航线被破坏,随着越来越多的航线被破坏,探险飞船又不能及时回复这些航线,可见两个星球之间的关键航线会越来越多。 假设在上图中,航线4-2(从4号星球到2号星球)被破坏。此时,1号与5号星球之间的关键航线就有3条:1-3,3-4,4-5。 小联的任务是,不断关注航线被破坏的情况,并随时给出两个星球之间的关键航线数目。现在请你帮助完成。

    Input

    第一行有两个整数N,M。表示有N个星球(1< N < 30000),初始时已经有M条航线(1 < M < 100000)。随后有M行,每行有两个不相同的整数A、B表示在星球A与B之间存在一条航线。接下来每行有三个整数C、A、B。C为1表示询问当前星球A和星球B之间有多少条关键航线;C为0表示在星球A和星球B之间的航线被破坏,当后面再遇到C为1的情况时,表示询问航线被破坏后,关键路径的情况,且航线破坏后不可恢复; C为-1表示输入文件结束,这时该行没有A,B的值。被破坏的航线数目与询问的次数总和不超过40000。

    Output

    对每个C为1的询问,输出一行一个整数表示关键航线数目。 注意:我们保证无论航线如何被破坏,任意时刻任意两个星球都能够相互到达。在整个数据中,任意两个星球之间最多只可能存在一条直接的航线。

    Sample Input

    5 5
    1 2
    1 3
    3 4
    4 5
    4 2
    1 1 5
    0 4 2
    1 5 1
    -1

    Sample Output

    1
    3

    HINT

     

    Source

    时间倒流

    首先把所有的操作全都读进来

    然后把最终形态的树建出来,

    这样删边操作就变成了加边操作

    维护联通性用LCT,缩点的话用并查集维护祖先,然后暴力改祖先即可

    access操作稍微有一些改动,具体看代码吧

    // luogu-judger-enable-o2
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int MAXN = 1e5+10;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
        return x * f;
    }
    int N, M;
    int father[MAXN];
    int find(int x) {
        if(father[x] == x) return father[x];
        else return father[x] = find(father[x]);
    }
    int unionn(int x, int y) {
        int fx = find(x), fy = find(y);
        father[fx] = fy;
    }
    #define ls(x) T[x].ch[0]
    #define rs(x) T[x].ch[1]
    #define fa(x) T[x].f
    struct node {
        int f, ch[2], r, siz;
    }T[MAXN];
    int ident(int x) {
        return T[fa(x)].ch[0] == x ? 0 : 1;
    }
    void update(int x) {
        T[x].siz = T[ls(x)].siz + T[rs(x)].siz + 1;
    }
    void pushdown(int x) {
        if(T[x].r) {
            T[ls(x)].r ^= 1, T[rs(x)].r ^= 1;
            swap(ls(x), rs(x));
            T[x].r = 0;
        }
    }
    void connect(int x, int fa, int how) {
        T[x].f = fa;
        T[fa].ch[how] = x;
    }
    bool IsRoot(int x) {
        return T[fa(x)].ch[0] != x && T[fa(x)].ch[1] != x;
    }
    void rotate(int x) {
        int Y = fa(x), R = fa(Y), Yson = ident(x), Rson = ident(Y);
        int B = T[x].ch[Yson ^ 1];
        T[x].f = R;
        if(!IsRoot(Y)) connect(x, R, Rson);
        connect(B, Y, Yson);
        connect(Y, x, Yson ^ 1);
        update(Y);update(x);
    }
    int st[MAXN];
    void splay(int x) {
        int y = x, top = 0;
        st[++top] = y;
        while(!IsRoot(y)) st[++top] = y = fa(y);
        while(top) pushdown(st[top--]);
        for(int y = fa(x); !IsRoot(x); rotate(x), y = fa(x))
            if(!IsRoot(y))
                rotate( ident(x) == ident(y) ? y : x);
    }
    void access(int x) {
        for(int y = 0; x;y = x, x = fa(y) = find(fa(x)))//这里要改一下 
            splay(x), rs(x) = y, update(x);
    }
    void makeroot(int x) {
        access(x); splay(x);
        T[x].r ^= 1;
    }
    void link(int x, int y) {
        //makeroot(x);
        T[x].f = y;
    }
    int findroot(int x) {
        //makeroot(x);
        access(x);splay(x);
        pushdown(x);
        while(ls(x)) pushdown(x = ls(x));
        splay(x);
        return x;
    }
    void delet(int x, int to) {
        if(x) father[x] = to, delet(ls(x), to), delet(rs(x), to);
    }
    void merge(int x, int y) {
        if(x == y) return ;
        makeroot(x);
        if(findroot(y) != x) {
            link(x, y); return ;
        }
        delet(rs(x), x); rs(x) = 0;
        update(x);
    }
    int split(int x,int y) {
        makeroot(x);
        access(y);
        splay(y);
    }
    struct Edge {
        int u,v;
        bool operator < (const Edge &rhs) const{
            return u < rhs.u || (u == rhs.u && v < rhs.v);
        }
    }E[MAXN];
    int vis[MAXN];
    struct Query {
        int opt, x, y;
    }Q[MAXN];
    int Qnum = 0;
    int ans[MAXN];
    int main() {
        #ifdef WIN32
        freopen("a.in", "r", stdin);
        #else
        //freopen("lane.in","r",stdin);
        //freopen("lane.out","w",stdout); 
        #endif
        N = read(), M = read();
        for(int i = 1; i <= N; i++) father[i] = i;
        for(int i = 1; i <= M; i++) {
            E[i].u = read(), E[i].v = read();
            if(E[i].u > E[i].v) swap(E[i].u, E[i].v);
        }
            
        sort(E + 1, E + M + 1);
        while(scanf("%d", &Q[++Qnum].opt) && Q[Qnum].opt != -1) {
            Q[Qnum].x = read(),
            Q[Qnum].y = read();
            if(Q[Qnum].x > Q[Qnum].y) swap(Q[Qnum].x, Q[Qnum].y);
            if(Q[Qnum].opt == 0)
                vis[lower_bound(E + 1, E + M + 1, (Edge){Q[Qnum].x, Q[Qnum].y} ) - E] = 1;    
        }
        Qnum--;
        for(int i = 1; i <= M; i++) 
            if(!vis[i]) 
                merge(
                        find(E[i].u), 
                        find(E[i].v)
                        );
        
        int ansnum = 0;
        for(int i = Qnum; i >= 1; i--) {
            Q[i].x = find(Q[i].x); Q[i].y = find(Q[i].y);
            if(Q[i].opt == 0) 
                merge(Q[i].x, Q[i].y);
            else 
                split(Q[i].x, Q[i].y), ans[++ansnum] = T[Q[i].y].siz - 1;  
        }
        while(ansnum) printf("%d
    ", ans[ansnum--]);
        return 0;
    }
  • 相关阅读:
    jquery的ztree操作
    原创-使用pywinauto和swapy-ob-0.4.3进行dotnet的winform程序控制(二)
    原创-使用pywinauto进行dotnet的winform程序控制(一)
    javascript“命名空间”的费曼输出[原创]
    IIS7上传4M文件以上文件出现“Post大小超出允许的限制”错误解决方法
    OpenProj打开不了或者提示”Failed to load Java VM Library”的错误的解决方案
    项目管理中,开始到完成、完成到开始、开始到开始、和完成到完成的关系图解
    我的项目管理之干系人分析在单位项目中的运用
    关于学习js的Promise的心得体会
    vue.js与后台模板引擎“双花括号”冲突时的解决办法
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/8972879.html
Copyright © 2011-2022 走看看