zoukankan      html  css  js  c++  java
  • 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
    ",ans);
        }
        return 0;
    }
    


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

  • 相关阅读:
    16.什么是面向对象编程?
    15.运动
    14.this指向和ES6常用内容
    13.正则表达式
    12.事件模型
    11.event事件对象
    10.BOM
    9.DOM
    拓扑排序学习(复习)笔记
    [Luogu] P1987 摇钱树
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4848304.html
Copyright © 2011-2022 走看看