BZOJ 2594 水管局长数据加强版

LCT维护最小生成树

要求两点路径最大的最小,首先想到的肯定是最小生成树,再加上有删边操作,那就得用LCT维护了。

可是对于cut一条边,我们要时刻维护图中的最小生成树,需要把之前被我们淘汰的边找回,那无法处理,所以我们考虑和并查集一样的技巧,倒着离线处理问题。

我们首先把询问中要删的边全部过滤掉,然后维护其他边产生的最小生成树,然后倒着回答每一个询问,把原来的删边变成连边,这样就方便多啦。

每一个cut需要比较边的大小,所以我们可以用map来记录每条边的编号。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 400005;
int n, m, q, tot, fa[N], mx[N], val[N], ch[N][2], id[N], rev[N], st[N], vis[N];
map<pair<int, int>, int> r;
vector<int> ans;
struct Edge {
    int u, v, w;
    bool operator < (const Edge &rhs) const {
        return w < rhs.w;
    }
}e[N<<2];
struct Query{ int opt, u, v; } query[N<<2];

int init(int v){
    ++tot;
    id[tot] = tot, val[tot] = mx[tot] = v;
    fa[tot] = ch[tot][0] = ch[tot][1] = rev[tot] = 0;
    return tot;
}

bool isRoot(int x){
    return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
}

void reverse(int x){
    rev[x] ^= 1, swap(ch[x][0], ch[x][1]);
}

void push_up(int x){
    int l = ch[x][0], r = ch[x][1];
    mx[x] = val[x], id[x] = x;
    if(mx[l] > mx[x]) mx[x] = mx[l], id[x] = id[l];
    if(mx[r] > mx[x]) mx[x] = mx[r], id[x] = id[r];
}

void push_down(int x){
    if(rev[x]){
        rev[x] ^= 1;
        if(ch[x][0]) reverse(ch[x][0]);
        if(ch[x][1]) reverse(ch[x][1]);
    }
}

void rotate(int x){
    int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
    ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
    if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
    fa[x] = z, fa[y] = x, ch[x][p] = y;
    push_up(y), push_up(x);
}

void splay(int x){
    int pos = 0; st[++pos] = x;
    for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
    while(pos) push_down(st[pos--]);
    while(!isRoot(x)){
        int y = fa[x], z = fa[y];
        if(!isRoot(y)) (ch[y][0] == x) ^ (ch[z][0] == y) ? rotate(x) : rotate(y);
        rotate(x);
    }
    push_up(x);
}

void access(int x){
    for(int p = 0; x; p = x, x = fa[x])
        splay(x), ch[x][1] = p, push_up(x);
}

void makeRoot(int x){
    access(x), splay(x), reverse(x);
}

int findRoot(int x){
    access(x), splay(x);
    while(ch[x][0]) push_down(x), x = ch[x][0];
    splay(x);
    return x;
}

bool isConnect(int x, int y){
    makeRoot(x);
    return findRoot(y) == x;
}

void link(int x, int y){
    makeRoot(x), fa[x] = y;
}

void split(int x, int y){
    makeRoot(x), access(y), splay(y);
}

int main(){

    n = read(), m = read(), q = read();
    for(int i = 1; i <= n; i ++) init(0);
    for(int i = 0; i < m; i ++){
        e[i].u = read(), e[i].v = read(), e[i].w = read();
        if(e[i].u > e[i].v) swap(e[i].u, e[i].v);
    }
    sort(e, e + m);
    for(int i = 0; i < m; i ++) r[make_pair(e[i].u, e[i].v)] = i;
    for(int i = 0; i < q; i ++){
        query[i].opt = read(), query[i].u = read(), query[i].v = read();
        if(query[i].u > query[i].v) swap(query[i].u, query[i].v);
        if(query[i].opt == 2) vis[r[make_pair(query[i].u, query[i].v)]] = true;
    }
    int cnt = 0;
    for(int i = 0; i < m; i ++){
        if(cnt == n - 1) break;
        if(vis[i]) continue;
        int u = e[i].u, v = e[i].v, t = init(e[i].w);
        if(isConnect(u, v)) continue;
        link(u, t), link(t, v), cnt ++;
    }
    for(int i = q - 1; i >= 0; i --){
        int u = query[i].u, v = query[i].v, opt = query[i].opt;
        if(opt == 1){
            split(u, v);
            ans.push_back(mx[v]);
        }
        else{
            split(u, v);
            int d = r[make_pair(u, v)];
            if(e[d].w > mx[v]) continue;
            int tmp = id[v], t = init(e[d].w);
            splay(tmp);
            fa[ch[tmp][0]] = fa[ch[tmp][1]] = 0;
            ch[tmp][0] = ch[tmp][1] = 0;
            link(u, t), link(t, v);
        }
    }
    for(int i = ans.size() - 1; i >= 0; i --){
        printf("%d\n", ans[i]);
    }
    return 0;
}
posted @ 2019-04-21 18:38  清楚少女ひなこ  阅读(134)  评论(0编辑  收藏  举报