HDU 2120 Ice_cream's world I(并检查集合)

职务地址:HDU 2120

这题尽管字数不多,但就是看不懂。

意思是求最多有多少个被墙围起来的区域。显然就是求环的个数。然后用并查集求环个数就能够了。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
int bin[2000];
int find1(int x)
{
    return bin[x]==x?x:bin[x]=find1(bin[x]);
}
int main()
{
    int n, m, ans, a, b, f1, f2, i;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        for(i=0;i<n;i++)
            bin[i]=i;
        while(m--)
        {
            scanf("%d%d",&a,&b);
            f1=find1(bin[a]);
            f2=find1(bin[b]);
            if(f1==f2)
                ans++;
            else
                bin[f2]=f1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


版权声明:本文博主原创文章,博客,未经同意不得转载。

posted @ 2015-09-30 09:49  phlsheji  阅读(165)  评论(0编辑  收藏  举报