hdu 2120

题意:求连通块中有多少个环。

题解:如果两数的祖先相同则总数加1,否则合并。

代码:

#include <cstdio>
int pre[2100],sum[2100],tot;
int
find(int x){
    int
r = x;
    while
(pre[r] != r){
        r = pre[r];
    }

    int
i = x, j;
    while
(i != r){
        j = pre[i];
        pre[i] = r;
        i = j;
    }

    return
r;
}

void
join(int x,int y){
    int
fx=find(x);
    int
fy=find(y);
    if
(fx!=fy)
        pre[fy]=fx;
    else
tot++;
}

int main(){
    int
n,m;
    while
(scanf("%d%d",&n,&m)!=EOF){
        int
a,b;
        for
(int i = 0; i < n; i++)
        {

            pre[i] = i;
        }

        tot=0;
        for
(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            join(a,b);
        }

        printf("%d\n",tot);
    }

    return
0;
}

时间: 2024-11-01 12:30:58

hdu 2120的相关文章

hdu 2120 Ice_cream&#39;s world I

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2120 Ice_cream's world I Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream wants award land to diligent ACMers. So there are some watchtowers are s

HDU 2120 Ice_cream&#39;s world I【并查集】

解题思路:给出n对点的关系,求构成多少个环,如果对于点x和点y,它们本身就有一堵墙,即为它们本身就相连,如果find(x)=find(y),说明它们的根节点相同,它们之间肯定有直接或间接的相连,即形成环 样例的示意图 共3个环 Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 642    Acce

hdu 2120 Ice_cream&#39;s world I(判断是否有环,简单的并查集)

Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 603    Accepted Submission(s): 347 Problem Description ice_cream's world is a rich country, it has many fertile lands. Today,

HDU 2120 Ice_cream&#39;s world I(并查集)

题目地址:HDU 2120 这题虽然字数不多,但就是看不懂..意思是求最多有多少个被墙围起来的区域.显然就是求环的个数.然后用并查集求环个数就可以了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h>

并查集 HDU 2120

其实就是判断新加的点是否在同一个集合里面 代码虐我千百遍,我待代码入初恋 Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 583    Accepted Submission(s): 333 Problem Description ice_cream's world is a rich count

HDOJ Ice_cream&#39;s world I 2120【并查集判断成环】

Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 848    Accepted Submission(s): 494 Problem Description ice_cream's world is a rich country, it has many fertile lands. Today,

HDU——T 2119 Matrix

http://acm.hdu.edu.cn/showproblem.php?pid=2119 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3097    Accepted Submission(s): 1429 Problem Description Give you a matrix(only contains 0 or 1),ev

hdu 1208 Pascal&#39;s Travels (子状态继承dp)

Pascal's Travels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1774    Accepted Submission(s): 781 Problem Description An n x n game board is populated with integers, one nonnegative integer

HDU 1208 跳格子题(很经典,可以有很多变形)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1208 Pascal's Travels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2795    Accepted Submission(s): 1298 Problem Description An n x n game board