天天看點

[WC2006]水管局長【LCT】

題目連結

  就是說,有個水管(人),他要打開水管(物),使得水能從u流到v,然後每個水管打開需要時間,問能讓水從u流到v,最少需要的時間,那麼其實也就是路徑上水管的最大時間了,現在我們要讓這個最大值最小。

  但是,有删除操作诶!那麼,既然删除不會重複,我們不妨把删除當作插入來做,除了永久邊以外,剩下的邊從時間戳從後往前插入,不就相當于是删除了。

  于是,實際上還是維護一個最小權生成樹了,遇到一條邊可以替換環上的最大邊,就直接替換了,這樣貪心的去找,就可以了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, M, Q;
namespace LCT
{
    int fa[maxN], c[maxN][2];
    int r[maxN];
    pair<int, int> v[maxN], mx[maxN];
    bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
    void pushup(int x)
    {
        mx[x] = max(max(mx[c[x][0]], mx[c[x][1]]), v[x]);
    }
    void pushr(int x) { swap(c[x][0], c[x][1]); r[x] ^= 1; }
    void pushdown(int x)
    {
        if(r[x])
        {
            if(c[x][0]) pushr(c[x][0]);
            if(c[x][1]) pushr(c[x][1]);
            r[x] = 0;
        }
    }
    void Rotate(int x)
    {
        int y = fa[x], z = fa[y], k = c[y][1] == x;
        if(!isroot(y)) c[z][c[z][1] == y] = x;
        fa[x] = z;
        c[y][k] = c[x][k ^ 1];
        fa[c[x][k ^ 1]] = y;
        c[x][k ^ 1] = y;
        fa[y] = x;
        pushup(y);
        pushup(x);
    }
    int Stap[maxN];
    void Splay(int x)
    {
        int y = x, z = 0;
        Stap[++z] = y;
        while(!isroot(y)) Stap[++z] = y = fa[y];
        while(z) pushdown(Stap[z--]);
        while(!isroot(x))
        {
            y = fa[x]; z = fa[y];
            if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
            Rotate(x);
        }
    }
    void Access(int x)
    {
        int y = 0;
        while(x)
        {
            Splay(x);
            c[x][1] = y;
            pushup(x);
            y = x;
            x = fa[x];
        }
    }
    void makeroot(int x)
    {
        Access(x);
        Splay(x);
        pushr(x);
    }
    int findroot(int x)
    {
        Access(x);
        Splay(x);
        while(c[x][0])
        {
            pushdown(x);
            x = c[x][0];
        }
        Splay(x);
        return x;
    }
    void Split(int x, int y)
    {
        makeroot(x);
        Access(y);
        Splay(y);
    }
    void link(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x)
        {
            fa[x] = y;
        }
    }
    void cut(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x || fa[y] != x || c[y][0]) return;
        fa[y] = c[x][1] = 0;
        pushup(x);
    }
};
using namespace LCT;
bool del[maxN] = {false};
struct Edge
{
    int u, v, w;
    Edge(int a=0, int b=0, int c=0):u(a), v(b), w(c) {}
} E[maxN];
map<pair<int, int>, int> mp;
struct Question
{
    int op, u, v;
    Question(int a=0, int b=0, int c=0):op(a), u(b), v(c) {}
} ques[maxN];
vector<ll> ans;
int root[maxN];
inline int fid(int x) { return x == root[x] ? x : root[x] = fid(root[x]); }
int main()
{
    v[0] = make_pair(0, 0);
    scanf("%d%d%d", &N, &M, &Q);
    for(int i=1; i<=N; i++) root[i] = i;
    for(int i=1, u, v, w; i<=M; i++)
    {
        scanf("%d%d%d", &u, &v, &w);
        if(u > v) swap(u, v);
        E[i] = Edge(u, v, w);
        mp[make_pair(u, v)] = i;
        LCT::v[N + i] = make_pair(w, i);
    }
    for(int i=1, op, u, v, id; i<=Q; i++)
    {
        scanf("%d%d%d", &op, &u, &v);
        ques[i] = Question(op - 1, u, v);
        if(u > v) swap(u, v);
        if(op == 2)
        {
            id = mp[make_pair(u, v)];
            del[id] = true;
            ques[i].op = id;
        }
    }
    for(int i=1, u, v, fu, fv, id; i<=M; i++)
    {
        if(del[i]) continue;
        u = E[i].u; v = E[i].v;
        fu = fid(u); fv = fid(v);
        if(fu == fv)
        {
            Split(u, v);
            if(E[i].w < mx[v].first)
            {
                id = mx[v].second;
                cut(E[id].u, N + id);
                cut(E[id].v, N + id);
                link(u, N + i);
                link(v, N + i);
            }
        }
        else
        {
            root[fu] = fv;
            link(u, N + i);
            link(v, N + i);
        }
    }
    for(int i=Q, u, v, w, id; i>=1; i--)
    {
        if(ques[i].op)
        {
            u = E[ques[i].op].u; v = E[ques[i].op].v; w = E[ques[i].op].w;
            Split(u, v);
            if(w < mx[v].first)
            {
                id = mx[v].second;
                cut(E[id].u, N + id);
                cut(E[id].v, N + id);
                link(u, N + ques[i].op);
                link(v, N + ques[i].op);
            }
        }
        else
        {
            u = ques[i].u; v = ques[i].v;
            Split(u, v);
            ans.push_back(mx[v].first);
        }
    }
    int len = (int)ans.size();
    for(int i=len - 1; i>=0; i--) printf("%lld\n", ans[i]);
    return 0;
}