Information
- ID
- 273
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 6
- Tags
- # Submissions
- 30
- Accepted
- 20
- Uploaded By
#include<iostream>
using namespace std;
const int N=100010;
int p[N];
int find(int x)
{
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
void merge(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx!=yy) p[xx]=yy;
}
int main(){
int n,m;
while(cin>>n>>m)
{
for(int i=1;i<=n;i++) p[i]=i;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
merge(x,y);
}
int sum=0;
for(int i=1;i<=n;i++)
if(p[i]==i)
sum++;
cout<<sum-1<<endl;
}
return 0;
}
By signing up a 追梦算法网 universal account, you can submit code and join discussions in all online judging services provided by us.