Uva 5031 Graph and Queries(Treap)

本文介绍了一种解决图论中动态连通性问题的方法,通过逆向处理操作并使用Treap数据结构来高效地处理边的删除及节点权值的修改等查询,实现了较低的时间复杂度。

题意:给一个n个点m条边的无向图,有三种操作:删除一条边;查询与节点X联通的弟k大的权值;将节点X的权值改为V;求最终查询的权值和的平均值。


思路:参照lrj书上的,将命令倒过来解题,建立多颗Treap,合并的时候采用启发式合并,时间复杂度就能降低。


#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long ll;
const int maxnode = 5 * 1e5 + 10;
const int maxq = 1e6 + 10;
const int maxn = 2 * 1e4 + 10;
const int maxm = 6 * 1e4 + 10;
using namespace std;

struct treap {
    int root[maxn], treapcnt, key[maxnode], pr[maxnode];
    int ch[maxnode][2], cnt[maxnode], sz[maxnode];
    void init () {
        memset(root, 0, sizeof root);
        memset(sz, 0, sizeof sz);
        memset(cnt, 0, sizeof cnt);
        treapcnt = 1;
        sz[0] = 0;
    }
    ///更新孩子的大小
    void update(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + cnt[x]; }
    ///旋转
    void _rotate(int &x, int t) {
        int y = ch[x][t];
        ch[x][t] = ch[y][t ^ 1];
        ch[y][t ^ 1] = x;
        update(x); update(y);
        x = y;
    }
    ///插入操作
    void __insert(int &x, int k) {
        if(x) {
            if(key[x] == k) cnt[x]++;
            else {
                int t = key[x] < k;
                __insert(ch[x][t], k);
                if(pr[ch[x][t]] < pr[x]) _rotate(x, t);
            }
        } else {  ///申请新节点
            x = treapcnt++;
            key[x] = k;
            cnt[x] = 1;
            pr[x] = rand();
            ch[x][0] = ch[x][1] = 0;
        }
        update(x);
    }
    ///删除操作
    void __erase(int &x, int k) {
        if(!x) return ;
        if(key[x] == k) {
            if(cnt[x] > 1) cnt[x]--;
            else {
                int ls = ch[x][0], rs = ch[x][1];
                if(!ls && !rs) { x = 0; return ; } ///叶节点直接删除
                int t = pr[ls] > pr[rs];
                _rotate(x, t ^ 1); __erase(x, k);
            }
        } else __erase(ch[x][key[x] < k], k);
        update(x);
    }
    ///获得排名
    int __getKth(int &x, int k) {
        if(x == 0) return 0;
        int siz = sz[ch[x][0]];
        if(k <= siz) return __getKth(ch[x][0], k);
        k -= cnt[x] + siz;
        if(k <= 0) return key[x];
        return __getKth(ch[x][1], k);
    }
} tp;

struct CMD {
    char op;
    int x, k, la;
    void input() {
        if(op == 'D') scanf("%d", &x);
        else scanf("%d %d", &x, &k);
    }
} cmd[maxq];
int n, m, w[maxn], mov[maxm], kase = 1;
int from[maxm], to[maxm], p[maxn];
char s[10];

int findset(int x) { return x == p[x] ? x : p[x] = findset(p[x]); }

void merg(int &x, int y) {
    while(tp.cnt[y]--) tp.__insert(x, tp.key[y]);
    int ls = tp.ch[y][0], rs = tp.ch[y][1];
    if(ls) merg(x, ls);
    if(rs) merg(x, rs);
}

int main() {
    while(scanf("%d %d", &n, &m) && n) {
        tp.init();
        memset(mov, 0, sizeof mov);
        for(int i = 1; i <= n; i++) p[i] = i;
        for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
        for(int i = 1; i <= m; i++) scanf("%d %d", &from[i], &to[i]);
        int q = 0;
        while(scanf("%s", s) && s[0] != 'E') {
            cmd[q].op = s[0];
            cmd[q].input();
            if(s[0] == 'D') mov[cmd[q].x] = 1;
            if(s[0] == 'C') {
                cmd[q].la = w[cmd[q].x];
                w[cmd[q].x] = cmd[q].k;
            }
            q++;
        }
        for(int i = 1; i <= m; i++) {
            if(mov[i]) continue;
            int nx = findset(from[i]);
            int ny = findset(to[i]);
            p[nx] = ny;
        }

        for(int i = 1; i <= n; i++) {
            int rt = findset(i);
            tp.__insert(tp.root[rt], w[i]);
        }

        double sum = 0;
        int tal = 0;
        for(int i = q - 1; i >= 0; i--) {
            char ch = cmd[i].op;
            if(ch == 'D') {
                int xx = cmd[i].x;
                int f = from[xx], t = to[xx];
                int nf = findset(f), nt = findset(t);
                if(nf == nt) continue;
                if(tp.sz[tp.root[nf]] > tp.sz[tp.root[nt]]) {
                    p[nt] = nf; merg(tp.root[nf], tp.root[nt]);
                } else { p[nf] = nt; merg(tp.root[nt], tp.root[nf]); }
            } else if(ch == 'C') {
                int xx = cmd[i].x;
                int kk = cmd[i].la;
                int pre = findset(xx);
                tp.__erase(tp.root[pre], w[xx]);
                tp.__insert(tp.root[pre], kk);
                w[xx] = kk;
            } else {
                int nx = findset(cmd[i].x);
                int t = tp.sz[tp.root[nx]];
                tal++;
                if(cmd[i].k > t || cmd[i].k < 1) continue;
                sum += tp.__getKth(tp.root[nx], t - cmd[i].k + 1);
            }
        }
        printf("Case %d: %f\n", kase++, sum / tal);
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值