#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int p[10002];
int ra[10002] = {0};
void init(int n)
{
for (int i = 0; i <= n; i++)
p[i] = i;
}
int fd(int x)
{
if (x != p[x])
p[x] = fd(p[x]);
return p[x];
}
void ui(int x, int y)
{
int fx = fd(x);
int fy = fd(y);
if (fx == fy)
return;
if (ra[fx] > ra[fy]) {
p[fy] = fx;
if (ra[fy]+1 > ra[fx])
ra[fx] = ra[fy]+1;
} else {
p[fx] = fy;
if (ra[fx]+1 > ra[fy])
ra[fy] = ra[fx]+1;
}
}
int main()
{
int n, m, v1, v2;
while (cin>>n, n)
{
init(n);
cin>>m;
for (int i = 0; i < m; i++)
{
cin>>v1>>v2;
ui(v1, v2);
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
if (p[i] == i)
ans++;
}
cout<<ans-1<<endl;
}
}
hdu 1232 畅通工程
最新推荐文章于 2020-02-04 11:17:35 发布