首页 > 其他 > 详细

带偏移量的并查集

时间:2018-07-08 17:07:09      阅读:196      评论:0      收藏:0      [点我收藏+]
//带偏移量的并查集≈并查集补集 
//维护各点到父节点的距离 可用来分类 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int n,f[1001],d[1001];
int find(int x)
{
    if(x==f[x])
        return x;
    d[x]+=d[f[x]];
    return f[x]=find(f[x]);
}
int get(int x,int y)
{
    int xx=find(x);
    int yy=find(y);
    if(xx!=yy)
        f[xx]=yy;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        f[i]=i; 
    for(int i=1;i<=n;i++)
    {
        int u,v;
        cin>>u>>v;
        get(u,v);
    }
    return 0; 
} 

 

带偏移量的并查集

原文:https://www.cnblogs.com/water-radish/p/9280539.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!