bzoj 2594: 水管局长数据加强版 Link-Cut-Tree

题目:

Description
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的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短)。

题解:

我们先不考虑水管的报废
那么在每个时刻,答案一定存在于最小生成树上
所以可我们可以考虑时刻维护最小生成树然后在最小生成树上求最大边
所以我们现在要考虑的是如果树上的某一条边被删除,应该选择那一条边来弥补.
我们发现这很简单不可做
所以我们考虑把所有的询问离线,改为倒序加入边,这样我们就可以瞎搞了.
网上的题解很多了,我就说个大概,如果都知道离线维护最小生成树了还不知道怎么搞...
那还是先不要捉这道题了

好不容易卡时过了..人傻自带大常数啊...

#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
    x=0;char ch;bool flag = false;
    while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
    while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 100001;
const int maxm = 1000001;
typedef pair<int,int> pa;
namespace lct{
    struct Node{
        Node *ch[2],*fa;
        int tag,w,mx,id,mxid;
        inline void update(){
            mx = max(max(ch[0]->mx,ch[1]->mx),w);
            if(mx == ch[0]->mx) mxid = ch[0]->mxid;
            if(mx == ch[1]->mx) mxid = ch[1]->mxid;
            if(mx == w) mxid = id;
        }
    }*null;
    inline void init(){
        null = new Node();null->id = 0;
        null->w = null->mx = null->tag = null->mxid = 0;
        null->ch[0] = null->ch[1] = null->fa = null;
    }
    inline Node* newNode(const int &val,const int &id){
        Node *p = new Node();p->w = p->mx = val;p->id = p->mxid = id;
        p->ch[0] = p->ch[1] = p->fa = null;p->tag = 0;
        return p;
    }
    inline void rotate(Node *p,Node *x){
        int k = p == x->ch[1];
        Node *y = p->ch[k^1],*z = x->fa;
        if(z->ch[0] == x) z->ch[0] = p;
        if(z->ch[1] == x) z->ch[1] = p;
        if(y != null) y->fa = x;
        p->fa = z;p->ch[k^1] = x;
        x->fa = p;x->ch[k] = y;
        x->update();p->update();
    }
    inline void push_down(Node* p){
        if(p == null || p->tag == 0) return;
        if(p->ch[0] != null) p->ch[0]->tag ^= 1;
        if(p->ch[1] != null) p->ch[1]->tag ^= 1;
        swap(p->ch[0],p->ch[1]);p->tag = 0;
    }
    inline bool isroot(Node* p){
        return (p == null) || (p->fa->ch[0] != p && p->fa->ch[1] != p);
    }
    inline void Splay(Node *p){
        push_down(p);
        while(!isroot(p)){
            Node *x = p->fa,*y = x->fa;
            push_down(y);push_down(x);push_down(p);
            if(isroot(x)) rotate(p,x);
            else if((x->ch[0] == p)^(y->ch[0] == x)) rotate(p,x),rotate(p,y);
            else rotate(x,y),rotate(p,x);
        }p->update();
    }
    inline void Access(Node *x){
        for(Node *y = null;x != null;y = x,x=x->fa)
            Splay(x),x->ch[1] = y,x->update();
    }
    inline void makeRoot(Node *x){
        Access(x);
        Splay(x);x->tag ^= 1;
    }
    inline void link(Node *u,Node *v){
        for(Node *y = null,*x = u;x != null;y = x,x=x->fa)
            Splay(x),x->ch[1] = y,x->update();
        Splay(u);u->tag ^= 1;
        u->fa = v;
    }
    inline void cut(Node *u,Node *v){
                for(Node *y = null,*x = u;x != null;y = x,x=x->fa)
            Splay(x),x->ch[1] = y,x->update();
        Splay(u);u->tag ^= 1;
                for(Node *y = null,*x = v;x != null;y = x,x=x->fa)
            Splay(x),x->ch[1] = y,x->update();
 
        Splay(v);
        v->ch[0] = v->ch[0]->fa = null;
        v->update();
    }
    inline pa query(Node *u,Node *v){
                for(Node *y = null,*x = u;x != null;y = x,x=x->fa)
            Splay(x),x->ch[1] = y,x->update();
        Splay(u);u->tag ^= 1;
                for(Node *y = null,*x = v;x != null;y = x,x=x->fa)
            Splay(x),x->ch[1] = y,x->update();Splay(v);
        return make_pair(v->mx,v->mxid);
    }
}
int nodecnt;
map<pa,int> mp;
struct Node{
    int u,v,d,num;
}e[maxm];
inline bool cmp(const Node &a,const Node &b){return a.d < b.d;}
inline bool kmp(const Node &a,const Node &b){return a.num < b.num;}
bool vis[maxm];
lct::Node *p[maxn+maxm];
int mp_pos[maxm];
struct cmd{
    int op,u,v;
}q[maxn];
int fa[maxn],sta[maxn];
inline int find(const int &x){return fa[x] == x ? x : fa[x] = find(fa[x]);}
int main(){
    lct::init();
    int n,m,Q;read(n);read(m);read(Q);
    for(int i=1;i<=n;++i){
        p[++nodecnt] = lct::newNode(0,-2147483647);
        fa[i] = i;
    }
    for(int i=1;i<=m;++i){
        read(e[i].u);read(e[i].v);read(e[i].d);
        if(e[i].u > e[i].v) swap(e[i].u,e[i].v);
        mp[make_pair(e[i].u,e[i].v)] = e[i].num = i;
    }
    for(int i=1;i<=Q;++i){
        read(q[i].op);read(q[i].u);read(q[i].v);
        if(q[i].u > q[i].v) swap(q[i].u,q[i].v);
        if(q[i].op == 2) vis[mp[make_pair(q[i].u,q[i].v)]] = true;
    }sort(e+1,e+m+1,cmp);
    for(int i=1,fx,fy,ct = 0;i<=m;++i){
        if(vis[e[i].num]) continue;
        fx = find(e[i].u);
        fy = find(e[i].v);
        if(fx == fy) continue;
        p[++nodecnt] = lct::newNode(e[i].d,e[i].num);
        lct::link(p[e[i].u],p[nodecnt]);
        lct::link(p[nodecnt],p[e[i].v]);
        mp_pos[e[i].num] = nodecnt;
        fa[fx] = fy;if(++ct == n-1) break;
    }sort(e+1,e+m+1,kmp);
    pa x;
    for(int i = Q,id;i>=1;--i){
        if(q[i].op == 1) sta[++sta[0]] = lct::query(p[q[i].u],p[q[i].v]).first;
        else{
            x = query(p[q[i].u],p[q[i].v]);
            id = mp[make_pair(q[i].u,q[i].v)];
            if(e[id].d >= x.first) continue;
            lct::cut(p[mp_pos[x.second]],p[e[x.second].u]);
            lct::cut(p[mp_pos[x.second]],p[e[x.second].v]);
            p[++nodecnt] = lct::newNode(e[id].d,id);
            mp_pos[id] = nodecnt;
            lct::link(p[nodecnt],p[q[i].u]);
            lct::link(p[nodecnt],p[q[i].v]);
        }
    }
    for(int i = sta[0];i;--i) printf("%d
",sta[i]);
    getchar();getchar();
    return 0;
}
原文地址:https://www.cnblogs.com/Skyminer/p/6533850.html