并查集

来源:互联网 发布:通达信日线数据下载 编辑:程序博客网 时间:2024/06/14 20:00

中午做了几个简单的并查集。

并查集简单的来说就是把几个独立的集合并起来的一种数据结构。

可以按秩合并,也可以直接路径压缩。

 

先附上一个题集。

并查集题集

 

我只切了4、5道,下面附上简单代码:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=37956#problem/F

入门题了。

把一些不相干的集合并起来,问最终集合元素最多的那个集合元素的总数。

直接套模板。

/***************************** author:crazy_石头* date:2014/01/15* algorithm:并查集* Pro:HDU 1856-More is better*****************************/#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>#include <queue>#include <map>#include <string>using namespace std;#define INF 1<<29#define eps 1e-8#define A system("pause")#define rep(i,h,n) for(int i=(h);i<=(n);i++)#define ms(a,b) memset((a),(b),sizeof(a))const int maxn=10000000+10;int p[maxn],sum[maxn];inline int find(int x){    return p[x]!=x?p[x]=find(p[x]):x;}int main(){    int n;    while(~scanf("%d",&n))    {        rep(i,1,maxn)        {            p[i]=i;            sum[i]=1;        }        rep(i,1,n)        {            int x,y;            scanf("%d%d",&x,&y);            x=find(x);            y=find(y);            if(x!=y)            {                p[x]=y;                sum[y]+=sum[x];            }        }        int best=-1;        rep(i,1,maxn)        if(p[i]==i)            best=max(best,sum[i]);        printf("%d\n",best);    }    return 0;}


 

 

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=37956#problem/E

这个题目说:n个独立城市起初各有1个龙珠,现在有两种操作,T A B,把A中的龙珠移动到B中, Q X,查询X在移动后的最终位置,以及X所在集合的龙珠总数,还有X被移动的次数;

 

思路:

在路径压缩的同时,记录一下结点的移动次数即可。

code:

/***************************** author:crazy_石头* date:2014/01/15* algorithm:并查集* Pro:HDU 3635-Dragon Balls*****************************/#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>#include <queue>#include <string>using namespace std;#define INF 1<<29#define eps 1e-8#define A system("pause")#define rep(i,h,n) for(int i=(h);i<=(n);i++)#define ms(a,b) memset((a),(b),sizeof(a))const int maxn=100000+10;int p[maxn],sum[maxn],hash[maxn];//hash记录移动球的次数,sum记录第i个城市最终龙珠总数目;inline int find(int x){    if(p[x]!=x)    {        int root=find(p[x]);        hash[x]+=hash[p[x]];        return p[x]=root;    }    else        return x;}int main(){    int test,n,m;    scanf("%d",&test);    rep(ii,1,test)    {        scanf("%d%d",&n,&m);        rep(i,1,n)        {            p[i]=i;            sum[i]=1;            hash[i]=0;        }        printf("Case %d:\n",ii);        while(m--)        {            char ch[2];            int x,y;            scanf("%s",ch);            if(ch[0]=='T')            {                scanf("%d%d",&x,&y);                int xx=find(x);                int yy=find(y);                if(xx!=yy)                {                    p[xx]=yy;                    sum[yy]+=sum[xx];                    sum[xx]=0;                    hash[xx]=1;                }            }            else if(ch[0]=='Q')            {                scanf("%d",&x);                int xx=find(x);                printf("%d %d %d\n",xx,sum[xx],hash[x]);            }        }    }    return 0;}


http://acm.hust.edu.cn/vjudge/contest/view.action?cid=37956#problem/Ahttp://acm.hust.edu.cn/vjudge/contest/view.action?cid=37956#problem/A

沙茶题。

/***************************** author:crazy_石头* date:2014/01/15* algorithm:并查集* Pro:LA 3664-X-Plosives*****************************/#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>#include <queue>#include <map>#include <string>using namespace std;#define INF 1<<29#define eps 1e-8#define A system("pause")#define rep(i,h,n) for(int i=(h);i<(n);i++)#define ms(a,b) memset((a),(b),sizeof(a))const int maxn=100000+10;int p[maxn];inline int find(int x){    return p[x]!=x?p[x]=find(p[x]):x;}int main(){    int x,y;    while(~scanf("%d",&x))    {        rep(i,1,maxn+1) p[i]=i;        int ret=0;        while(~x)        {            scanf("%d",&y);            x=find(x);            y=find(y);            if(x==y) ret++;            else p[x]=y;            scanf("%d",&x);        }        printf("%d\n",ret);    }    return 0;}


http://acm.hust.edu.cn/vjudge/contest/view.action?cid=37956#problem/B

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=37956#problem/B

在合并过程中,顺便维护一个距离数组,代表当前结点到根节点的距离即可。

关键代码:

const int maxn=100000+10;int p[maxn],d[maxn];//d数组表示该结点到根节点的距离;inline int find(int x)//压缩路径的同时维护一下距离数组;{    if(p[x]!=x)    {        int root=find(p[x]);        d[x]+=d[p[x]];        return p[x]=root;    }    else        return x;}


 

0 0
原创粉丝点击