您的位置:首页 > 其它

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

2014-04-05 12:04 423 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: