看看你的数组是不是约等于1000
或者下载一下第一个点
#include<iostream>
using namespace std;
int p[1003];
struct city
{
int x,y;
};
int f(int x)
{
if(p[x]!=x)
{
p[x] = f(p[x]);
}
return p[x];
}
int main()
{
while(1)
{
int n,m;
cin>>n;
if(n==0)
{
break;
}
cin>>m;
city c[1003];
int cnt = 1;
for(int i = 1;i<=n;i++)
{
p[i] = i;
}
for(int i = 1;i<=m;i++)
{
cin>>c[i].x>>c[i].y;
}
for(int i = 1;i<=m;i++)
{
int a = f(c[i].x),b = f(c[i].y);
if(a!=b)
{
cnt++;
p[a] = b;
}
}
cout<<n-cnt<<endl;
}
}
like this
and
it
所以看见重边就开大点