现在的位置: 首页 > 综合 > 正文

hdu 1232

2013年08月05日 ⁄ 综合 ⁄ 共 404字 ⁄ 字号 评论关闭

并查序问题,看代码

URL:http://acm.hdu.edu.cn/showproblem.php?pid=1232

#include<iostream>
using namespace std;
int f[1000],sum;
int find(int x)
{
 if(f[x]!=x)
  f[x]=find(f[x]);
 return f[x];
}
void make(int a,int b)
{
 int f1=find(a);
 int f2=find(b);
 if(f1!=f2)
 {
  f[f2]=f1;
  sum++;
 }
}
int main()
{
 int n,m,i;
 while(cin>>n&&n)
 {
  cin>>m;
  sum=0;
  for(i=1;i<=n;++i)
   f[i]=i;
  sum=0;
  for(i=1;i<=m;++i)
  {
   int a,b;
   scanf("%d%d",&a,&b);
   make(a,b);
  }
  cout<<n-1-sum<<endl;
 }
 return 0;
}

抱歉!评论已关闭.