zoukankan      html  css  js  c++  java
  • 洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)

    SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径,接着通过信息化的控制中心通知路径上的水管进入准备送水状态,等到路径上每一条水管都准备好了,供水公司就可以开始送水了。嘟嘟一次只能处理一项送水任务,等到当前的送水任务完成了,才能处理下一项。
    在处理每项送水任务之前,路径上的水管都要进行一系列的准备操作,如清洗、消毒等等。嘟嘟在控制中心一声令下,这些水管的准备操作同时开始,但由于各条管道的长度、内径不同,进行准备操作需要的时间可能不同。供水公司总是希望嘟嘟能找到这样一条送水路径,路径上的所有管道全都准备就绪所需要的时间尽量短。嘟嘟希望你能帮助他完成这样的一个选择路径的系统,以满足供水公司的要求。另外,由于MY市的水管年代久远,一些水管会不时出现故障导致不能使用,你的程序必须考虑到这一点。
    不妨将MY市的水管网络看作一幅简单无向图(即没有自环或重边):水管是图中的边,水管的连接处为图中的结点。
     

    Input

    输入文件第一行为3个整数:N, M, Q分别表示管道连接处(结点)的数目、目前水管(无向边)的数目,以及你的程序需要处理的任务数目(包括寻找一条满足要求的路径和接受某条水管坏掉的事实)。
    以下M行,每行3个整数x, y和t,描述一条对应的水管。x和y表示水管两端结点的编号,t表示准备送水所需要的时间。我们不妨为结点从1至N编号,这样所有的x和y都在范围[1, N]内。
    以下Q行,每行描述一项任务。其中第一个整数为k:若k=1则后跟两个整数A和B,表示你需要为供水公司寻找一条满足要求的从A到B的水管路径;若k=2,则后跟两个整数x和y,表示直接连接x和y的水管宣布报废(保证合法,即在此之前直接连接x和y尚未报废的水管一定存在)。
     

    Output

    按顺序对应输入文件中每一项k=1的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短)。
     

    Sample Input

    4 4 3
    1 2 2
    2 3 3
    3 4 2
    1 4 2
    1 1 4
    2 1 4
    1 1 4

    Sample Output

    2
    3

    【原题数据范围】
    N ≤ 1000
    M ≤ 100000
    Q ≤ 100000
    测试数据中宣布报废的水管不超过5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
     
    题解:
    很显然根据克鲁斯卡尔求最小生成树的思路下,对于最小生成树来说,树上任意两个点之间的非树边与原树边构成的环里,这条非树边肯定是最长的,进而可以推出最小生成树上任意两点之间的沿树边上的最大值是整张图中这两点之间最大值中最小的。
    所以问题转化成了动态求最小生成树
    显然删边不好做,就可以倒过来搞,故事就变成了加边
    加边这个东西相当于比较新加的这条边边权和新加边连接两点的树边上最大的边哪个大。
    如果原边大的话显然需要把新边加进去,也就是把旧边断开,新边连上
    边权显然不好维护,可以考虑把所有边都换成点,于是就边权换点权了。
    但是需要注意一点,这样子要cut两次,在cut的时候注意make_root,或者记录一下原点,因为原点会变qwq(为此调了半个上午)
     
    代码如下:
    #include<map>
    #include<set>
    #include<queue>
    #include<cmath>
    #include<stack>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define N 1100010
    #define mp make_pair
    #define lson ch[x][0]
    #define rson ch[x][1]
    #define pii pair<int,int>
    using namespace std;
    
    struct node
    {
        int from,to,w;
    } e[N];
    struct o
    {
        int from,to,kd,w,ori;
    } op[N];
    int n,m,k;
    map<pii,int> m1,m2;
    
    int cmp(node a,node b)
    {
        return a.w<b.w;
    }
    
    //lct start
    
    int f[N],ch[N][2],w[N],tag[N],sum[N];
    
    int not_root(int now)
    {
        int x=f[now];
        return lson==now||rson==now;
    }
    
    int push_up(int x)
    {
        sum[x]=x;
        if(e[sum[lson]].w>e[sum[x]].w)
        {
            sum[x]=sum[lson];
        }
        if(e[sum[rson]].w>e[sum[x]].w)
        {
            sum[x]=sum[rson];
        }
    }
    
    int rev(int x)
    {
        swap(lson,rson);
        tag[x]^=1;
    }
    
    int push_down(int x)
    {
        if(tag[x])
        {
            rev(lson);
            rev(rson);
            tag[x]^=1;
        }
    }
    
    int rotate(int x)
    {
        int y=f[x],z=f[y],kd=ch[y][1]==x,xs=ch[x][!kd];
        if(not_root(y)) ch[z][ch[z][1]==y]=x;
        ch[x][!kd]=y;
        ch[y][kd]=xs;
        if(xs) f[xs]=y;
        f[x]=z;
        f[y]=x;
        push_up(y);
    }
    
    int push_all(int x)
    {
        if(not_root(x))
        {
            push_all(f[x]);
        }
        push_down(x);
    }
    
    int splay(int x)
    {
        int y,z;
        push_all(x);
        while(not_root(x))
        {
            int y=f[x],z=f[y];
            if(not_root(y))
            {
                (ch[y][0]==x)^(ch[z][0]==y)?rotate(x):rotate(y);
            }
            rotate(x);
        }
        push_up(x);
    }
    
    int access(int x)
    {
        for(int y=0; x; y=x,x=f[x])
        {
            splay(x);
            rson=y;
            push_up(x);
        }
    }
    
    int make_root(int x)
    {
        access(x);
        splay(x);
        rev(x);
    }
    
    int split(int x,int y)
    {
        make_root(x);
        access(y);
        splay(y);
    }
    
    int find_root(int x)
    {
        access(x);
        splay(x);
        while(lson)
        {
            push_down(x);
            x=lson;
        }
        return x;
    }
    
    int link(int x,int y)
    {
        make_root(x);
        if(find_root(y)==x) return 0;
        f[x]=y;
        return 1;
    }
    
    int cut(int x,int y)
    {
        make_root(x);
        if(find_root(y)!=x||f[x]!=y||rson) return 0;
        f[x]=ch[y][0]=0;
        push_up(y);
        return 1;
    }
    
    int print(int x)
    {
        if(lson) print(lson);
        printf("%d ",x);
        if(rson) print(rson);
    }
    
    //lct end
    
    // dsu start
    
    int fa[N];
    
    int init()
    {
        for(int i=1; i<N; i++)
        {
            fa[i]=i;
        }
    }
    
    int find(int x)
    {
        if(fa[x]==x) return x;
        return fa[x]=find(fa[x]);
    }
    
    int unity(int x,int y)
    {
        int fx=find(x);
        int fy=find(y);
        if(fx==fy) return 0;
        fa[x]=y;
        return 1;
    }
    
    //dsu end
    
    int main()
    {
        init();
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].w);
    
        }
        sort(e+1,e+m+1,cmp);
        for(int i=1; i<=m; i++)
        {
            m2[mp(e[i].from,e[i].to)]=i;
            m2[mp(e[i].to,e[i].from)]=i;
        }
        int kd,from,to;
        for(int i=1; i<=k; i++)
        {
            scanf("%d%d%d",&op[i].kd,&op[i].from,&op[i].to);
            if(op[i].kd==2)
            {
                m1[mp(op[i].from,op[i].to)]=1;
                m1[mp(op[i].to,op[i].from)]=1;
                op[i].ori=m2[mp(op[i].from,op[i].to)];
                op[i].w=e[op[i].ori].w;
            }
        }
        for(int i=1; i<=m; i++)
        {
            if(m1[mp(e[i].from,e[i].to)]>0) continue;
            if(unity(e[i].from,e[i].to))
            {
                link(e[i].from+m,i);
                link(e[i].to+m,i);
            }
        }
        stack<int> ans;
        for(int i=k; i>=1; i--)
        {
            if(op[i].kd==1)
            {
                split(op[i].from+m,op[i].to+m);
                ans.push(e[sum[op[i].to+m]].w);
            }
            if(op[i].kd==2)
            {
                split(op[i].from+m,op[i].to+m);
                if(e[sum[op[i].to+m]].w>op[i].w)
                {
                    int gg=sum[op[i].to+m];
                    cut(e[sum[op[i].to+m]].from+m,gg);
                    cut(e[sum[op[i].to+m]].to+m,gg);
                    link(op[i].from+m,op[i].ori);
                    link(op[i].to+m,op[i].ori);
                }
            }
        }
        while(!ans.empty())
        {
            printf("%d
    ",ans.top());
            ans.pop();
        }
    }
     
     
  • 相关阅读:
    太赞了!两个技巧帮你记住复杂 Linux 命令!
    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
    Spring Cloud 学习 之 Spring Cloud Eureka(架构)
    Spring Cloud 学习 之 Spring Cloud Eureka(搭建)
    Spring Cloud 学习 之 Spring Cloud Eureka(概述)
    Spring Boot学习 之 Spring Boot Actuator(一)
    java基础篇 之 final关键字
    spring学习笔记(九)事务学习(上)
    JAVA基础篇 之 类的初始化
    JAVA基础篇 之 finalize()方法的作用
  • 原文地址:https://www.cnblogs.com/stxy-ferryman/p/9481657.html
Copyright © 2011-2022 走看看