[BZOJ]2733永无乡 做题笔记

10 篇文章 0 订阅

题目来源:http://www.lydsy.com/JudgeOnline/problem.php?id=2733
这题是Splay启发式合并。
启发式合并,感觉也像是一种暴力,把小的一堆拆开强行放到大的一堆里面。。
使用并查集来维护两个结点是否位于一颗子树上。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX=100009;
struct Splay {
    int a[MAX],f[MAX],s[MAX],fa[MAX];
    int ch[MAX][2];
    int find (int x) {
        if (x==fa[x]) return x;
        fa[x]=find(fa[x]);
        return fa[x];
    }
    void update (int x) {
        s[x]=s[ch[x][0]]+s[ch[x][1]]+1;
    }
    void rotate(int x) {
        int y=f[x],opt;
        if (ch[f[x]][0]==x) opt=0;
        else opt=1;
        ch[y][opt]=ch[x][!opt];
        if (ch[x][!opt]) f[ch[x][!opt]]=y;
        f[x]=f[y];
        if (f[y])
            if (ch[f[y]][0]==y) ch[f[y]][0]=x;
            else ch[f[y]][1]=x;
        f[y]=x,ch[x][!opt]=y;
        update(y),update(x);
    }
    void splay (int x,int to=0) {
        while (f[x]!=to) {
            if (f[f[x]]==to) rotate(x);
            else if ((ch[f[f[x]]][0]==f[x])==
                (ch[f[x]][0]==x))
            rotate(f[x]),rotate(x);
            else rotate(x),rotate(x);
        }
    }
    void insert (int x,int y) {
        while (1) {
            if (a[y]<=a[x]) {
                if (ch[x][0]) x=ch[x][0];
                else {
                    f[y]=x;
                    ch[x][0]=y;
                    break;
                }
            }
            else {
                if (ch[x][1]) x=ch[x][1];
                else {
                    f[y]=x;
                    ch[x][1]=y;
                    break;
                }
            }
        }
        update(x);
        splay(y);
    }
    void merge (int x,int y) {
        if (!y) return ;
        merge(x,ch[y][0]);
        merge(x,ch[y][1]);
        ch[y][0]=ch[y][1]=0;s[y]=1;
        insert(x,y);
    }
    void addedge (int x,int y) {
        int fx=find(x);
        int fy=find(y);
        if (fx!=fy) {
            fa[fy]=fx;
            splay(x);splay(y);
            if (s[x]<s[y]) swap(x,y);
            merge(x,y);
        }
    }
    int find_by_rank (int x,int k) {
        while (x) {
            if (s[ch[x][0]]+1==k) return x;
            if (k<s[ch[x][0]]+1) x=ch[x][0];
            else k-=s[ch[x][0]]+1,x=ch[x][1];
        }
        return 0;
    }
    void query (int x,int k) {
        splay(x);
        if (k>s[x]) { puts("-1");return ; }
        printf("%d\n",find_by_rank(x,k));
    }
}Sp;
int n,m,q;
int main () {
    int a,b;
    char ch;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) {
        scanf("%d",&Sp.a[i]);
        Sp.fa[i]=i;
        Sp.s[i]=1;
    }
    for (int i=1;i<=m;i++) {
        scanf("%d%d",&a,&b);
        Sp.addedge(a,b);
    }
    scanf("%d",&q);
    for (int i=1;i<=q;i++) {
        while ((ch=getchar())!='Q'&&ch!='B');
        if (ch=='Q') {
            scanf("%d%d",&a,&b);
            Sp.query(a,b);
        }
        else if (ch=='B') {
            scanf("%d%d",&a,&b);
            Sp.addedge(a,b);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值