就是问有多少个圈,简单的并查集,没啥好说的,心塞不开心
#include<iostream>
using namespace std;
int mapp[1000+5];
void s()
{
for(int i=0;i<1000+5;i++) mapp[i]=i;
}
int bfs(int x)
{
if(x!=mapp[x])
{
mapp[x]=bfs(mapp[x]);
}
return mapp[x];
}
int main()
{
int n,m;
while(cin>>n>>m)
{
s();
int sum=0;
while(m--)
{
int x,y;
cin>>x>>y;
x=bfs(x);
y=bfs(y);
if(x==y) sum++;
else mapp[x]=y;
}
cout<<sum<<endl;
}
return 0;
}