【并查集判环】hdu 2120 Ice_cream's world I

来源:互联网 发布:flowchart.js演示 编辑:程序博客网 时间:2024/05/22 04:59



#include <stdio.h>#include <map>#include <math.h>#include <algorithm>#include <string>#include <string.h>#include <iostream>#define mm(a) memset(a, 0, sizeof(a))using namespace std;int father[1111];int findd(int x){    if(x != father[x])        father[x] = findd(father[x]);    return father[x];}int main(){    int n, m;    while(scanf("%d%d", &n, &m) != EOF)    {        for(int i = 0; i < n; i ++)            father[i] = i;        int cnt = 0;        while(m--)        {            int a, b;            scanf("%d%d", &a, &b);            a = findd(a);            b = findd(b);            if(a != b)                father[a] = b;            else if(a == b)                cnt ++;        }        printf("%d\n", cnt);    }    return 0;}


0 0
原创粉丝点击