#include <bits/stdc++.h>
using namespace std;
int pre[2010];
int Find(int x);
int join(int x, int y);
int main()
{
int n, m;
while(cin >> n >> m)
{
for(int i = 1; i <= n; i++)
{
pre[i] = i;
}
for(int i = 1; i <= m; i++)
{
int x, y;
cin >> x >> y;
join(x, y);
}
int num = 0;
for(int i = 1; i <= n; i++)
{
if(pre[i] == i)
{
++num;
}
}
cout << num << endl;
}
return 0;
}
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;
}
int join(int x, int y)
{
int fx = Find(x), fy = Find(y);
if(fx != fy)
{
pre[fx] = fy;
}
}
SDUT 2805 大家快来A水题
最新推荐文章于 2018-12-01 14:31:07 发布