[BZOJ2594][WC2006][LCT][MST]水管局长数据加强版

[Problem Description]
    SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径,接着通过信息化的控制中心通知路径上的水管进入准备送水状态,等到路径上每一条水管都准备好了,供水公司就可以开始送水了。嘟嘟一次只能处理一项送水任务,等到当前的送水任务完成了,才能处理下一项。
    在处理每项送水任务之前,路径上的水管都要进行一系列的准备操作,如清洗、消毒等等。嘟嘟在控制中心一声令下,这些水管的准备操作同时开始,但由于各条管道的长度、内径不同,进行准备操作需要的时间可能不同。供水公司总是希望嘟嘟能找到这样一条送水路径,路径上的所有管道全都准备就绪所需要的时间尽量短。嘟嘟希望你能帮助他完成这样的一个选择路径的系统,以满足供水公司的要求。另外,由于MY市的水管年代久远,一些水管会不时出现故障导致不能使用,你的程序必须考虑到这一点。
    不妨将MY市的水管网络看作一幅简单无向图(即没有自环或重边):水管是图中的边,水管的连接处为图中的结点。
[Algorithm & Data Structure]
LCT MST
[Analysis]
这种一边查询一边删除的题目,通常都是倒着做……于是,我们就倒过来做……先将最后都没有被删除的边构造出一个MST,将每个边转换为连接边的两边的一个点,加入到LCT中去。Query的时候直接在LCT上找,如果要加边,所加入的边和原来的边一定构成了一个环,再在原来的LCT中查询一下这上面最大的边,如果大于加入的边,就拿新的边替换这一条最大的边
[Code]
/**************************************************************
    Problem: 2594
    User: gaotianyu1350
    Language: C++
    Result: Accepted
    Time:10488 ms
    Memory:31764 kb
****************************************************************/
 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
 
const int MAXN = 300000;
const int MAXM = 1000100;
const int INF  = 2100000000;
 
class lct_node
{
public:
    lct_node *ch[2], *pre;
    int val, maxx, loc, rev, self;
    lct_node();
    void Update()
    {
        maxx = val; loc = self;
        if (ch[0]->maxx > maxx)
            maxx = ch[0]->maxx, loc = ch[0]->loc;
        if (ch[1]->maxx > maxx)
            maxx = ch[1]->maxx, loc = ch[1]->loc;
    }
    void Pushdown()
    {
        if (rev)
        {
            lct_node *temp = ch[0]; ch[0] = ch[1]; ch[1] = temp;
            ch[0]->rev ^= 1; ch[1]->rev ^= 1;
            rev = 0;
        }
    }
    bool Root()
    {
        return !(pre->ch[0] == this || pre->ch[1] == this);
    }
    int Which()
    {
        if (Root()) return -1;
        return pre->ch[0] == this ? 0 : 1;
    }
    void Set(lct_node *child, int wh)
    {
        Pushdown();
        ch[wh] = child, child -> pre = this;
        Update();
    }
}lct[MAXN];
 
lct_node::lct_node()
{
    ch[0] = ch[1] = pre = lct;
    val = maxx = rev = 0;
}
 
inline void Rotate(lct_node *now)
{
    lct_node *oldfather = now->pre;
    lct_node *grand = oldfather->pre;
    oldfather->Pushdown(); now->Pushdown();
    bool isroot = oldfather->Root();
    int wh = now->Which();
    oldfather->Set(now->ch[wh ^ 1], wh);
    now->Set(oldfather, wh ^ 1);
    now->pre = grand;
    if (!isroot)
        grand->ch[grand->ch[0] == oldfather ? 0 : 1] = now;      
}
 
inline void Splay(lct_node *now)
{
    for (; !now->Root(); Rotate(now))
        if (!now->pre->Root())
            now->Which() == now->pre->Which() ? Rotate(now->pre) : Rotate(now);
}
 
inline lct_node* Access(lct_node *now)
{
    lct_node *last = lct;
    for (; now != lct; last = now, now = now->pre)
    {
        Splay(now);
        now->Set(last, 1);
    }
    return last;
}
 
inline void MakeRoot(lct_node *now)
{
    Access(now)->rev ^= 1;
    Splay(now);
}
 
inline void Link(int xx, int yy)
{
    lct_node *x = lct + xx, *y = lct + yy;
    MakeRoot(y);
    y->pre = x;
    Access(y);
}
 
inline void Cut(int xx, int yy)
{
    lct_node *x = lct + xx, *y = lct + yy;
    MakeRoot(x);
    Access(y);
    Splay(y);
    y->ch[0] = x->pre = lct;
    y->Update();
}
 
inline int Query(int xx, int yy, int &loc)
{
    lct_node *x = lct + xx, *y = lct + yy;
    MakeRoot(x);
    Access(y);
    Splay(y);
    loc = y->loc;
    return y->maxx;
}
 
struct QueryData
{
    int k, a, b, v;
}qvector[MAXN];
struct edge
{
    int x, y, v;
    bool operator < (const edge b) const
    {
        return (x == b.x && y < b.y) || x < b.x;
    }
}e[MAXM], duiying[MAXN];
int f[MAXN], n, m, q;
 
inline void swap(int &a, int &b)
{
    int temp = a; a = b; b = temp;
}
 
bool cmp(const edge a, const edge b)
{
    return a.v < b.v;
}
 
inline int Getfather(int x)
{
    return f[x] == x ? x : f[x] = Getfather(f[x]);
}
 
int getint()
{
    char ch = getchar();
    for ( ; ch > '9' || ch < '0'; ch = getchar());
    int tmp = 0;
    for ( ; '0' <= ch && ch <= '9'; ch = getchar())
    tmp = tmp * 10 + int(ch) - 48;
    return tmp;
} 
 
int main()
{
    //freopen("input.txt", "r", stdin);
    n = getint(); m = getint(); q = getint();
    for (int i = 1; i <= n; i++)
        f[i] = i;
    for (int i = 1; i <= m; i++)
    {
        e[i].x = getint();
        e[i].y = getint();
        e[i].v = getint();
        if (e[i].x > e[i].y) swap(e[i].x, e[i].y);
    }              
    sort(e + 1, e + 1 + m);
    for (int i = 1; i <= q; i++)
    {
        qvector[i].k = getint();
        qvector[i].a = getint();
        qvector[i].b = getint();         
        if (qvector[i].a > qvector[i].b)
            swap(qvector[i].a, qvector[i].b);
        if (qvector[i].k == 2)
        {
            edge *p = lower_bound(e + 1, e + 1 + m, (edge){qvector[i].a, qvector[i].b});
            qvector[i].v = p->v;
            p->v = INF;
        }
    }                
    sort(e + 1, e + 1 + m, cmp);
    int cnt = 0;
    for (int i = 1; i <= m; i++)
    {
        int fx = Getfather(e[i].x);
        int fy = Getfather(e[i].y);
        if (fx == fy) continue;
        cnt++;
        f[fx] = fy;
        duiying[cnt].x = e[i].x, duiying[cnt].y = e[i].y, duiying[cnt].v = e[i].v;
        lct[n + cnt].val = lct[n + cnt].maxx = e[i].v;
        lct[n + cnt].self = cnt;
        Link(e[i].x, n + cnt);
        Link(e[i].y, n + cnt);
        if (cnt == n - 1) break;
    }
    for (int i = q; i >= 1; i--)
    {
        int temp;
        if (qvector[i].k == 1)
            qvector[i].v = Query(qvector[i].a, qvector[i].b, temp);
        else
        {
            int maxedge = Query(qvector[i].a, qvector[i].b, temp);
            if (maxedge > qvector[i].v)
            {
                Cut(duiying[temp].x, n + temp);
                Cut(duiying[temp].y, n + temp);
                lct[n + temp].val = lct[n + temp].maxx = qvector[i].v;
                duiying[temp].x = qvector[i].a;
                duiying[temp].y = qvector[i].b;
                duiying[temp].v = qvector[i].v;
                Link(qvector[i].a, n + temp);
                Link(qvector[i].b, n + temp);              
            }
        }
    }
    for (int i = 1; i <= q; i++)
        if (qvector[i].k == 1)
            printf("%d\n", qvector[i].v);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值