ZOJ 2334左偏树+并查集

ZOJ 2334

题意很好理解……

这左偏树看了上交模板,但是不知道怎么用,研究了左偏树好久……才会一点点……

左偏树的操作都是建立在合并上,所以合并后的堆顶编号极其重要,我就是这里搞了半天,才知道这里错了。

然后又查了其他资料,才弄清楚,因为在合并中有:dist[x]=dist[r[x]]+1;,所以合并的编号应该是更新 root[right] 的。

参考博客:https://www.byvoid.com/blog/leftist-tree/

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define sc(a,b) scanf("%d%d",&a,&b)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 1000004
#define MN 1008
#define INF 100000007
#define eps 1e-7
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int f[MM],root[MM];  //root是初始每个左偏树的堆顶编号
int tot,v[MM],l[MM],r[MM],dist[MM];
int Merge(int x,int y)
{
    if(!x) return y;
    if(!y) return x;
    if(v[x]<v[y]) swap(x,y);
    r[x]=Merge(r[x],y);
    if(dist[l[x]]<dist[r[x]]) swap(l[x],r[x]);
    dist[x]=dist[r[x]]+1;
    return x;
}
int Init(int x)
{
    tot++;
    v[tot]=x;
    l[tot]=r[tot]=dist[tot]=0;
    return tot;
}
int Insert(int x,int y)
{
    return Merge(x,Init(y));
}
int Top(int x)
{
    return v[x];
}
int Pop(int x)
{
    return Merge(l[x],r[x]);
}
int find(int x)
{
    return f[x]==x?x:find(f[x]);
}
int solve(int a,int b)
{
    int xx,yy,p,x=find(a),y=find(b);
    if(x==y) return -1;
    f[x]=y;
    xx=Top(root[x]);;
    root[x]=Pop(root[x]);
    root[x]=Insert(root[x],xx/2);

    yy=Top(root[y]); //堆顶值
    root[y]=Pop(root[y]);
    root[y]=Insert(root[y],yy/2);
    root[y]=Merge(root[x],root[y]); //堆顶编号
    return v[root[y]];
}
int main()
{
    int a,b,i,m,n;
    while(~sca(n))
    {
        tot=0;
        for(i=1;i<=n;i++)
        {
            f[i]=i;
            sca(m);
            root[i]=0; //初始是在0编号插入
            root[i]=Insert(root[i],m);
        }
        sca(m);
        while(m--)
        {
            sc(a,b);
            pri(solve(a,b));
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值