当前位置: 代码迷 >> 综合 >> HDU---1213(How Many Tables,并查集判连通分支数)
  详细解决方案

HDU---1213(How Many Tables,并查集判连通分支数)

热度:81   发布时间:2024-01-22 02:02:00.0

题意:

判断连通分支数

题解:

     并查集


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<vector>
#include<functional>
#include<utility>
#include<set>
#include<map>
#include<cmath>using namespace std;const int maxn=1005;
int pa[maxn];
int findset(int x)
{return pa[x]==-1?x:pa[x]=findset(pa[x]);
}void unionset(int x, int y)
{int rx=findset(x);int ry=findset(y);if(rx==ry)return;pa[rx]=ry;
}int main()
{int t;cin>>t;while(t--){int n,m;cin>>n>>m;memset(pa,-1,sizeof(pa));while(m--){   int x,y;scanf("%d%d",&x,&y);unionset(x,y);}int ans=0;for(int i=1;i<=n;i++)if(pa[i]<0)ans++;cout<<ans<<endl;}}

  相关解决方案