HDU-1213 並查集裸題
阿新 • • 發佈:2019-02-17
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
#define DEBUG
const int maxn=110000+5,maxv=26,INF=0x3f3f3f3f,mod=100000000;
int n,m,p[maxn];
int find(int a){
return p[a]==a?a:find(p[a]);
}
int main(){
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t;
cin>>t;
while(t--){
memset(p,0,sizeof(p));
cin >>n>>m;
int a,b;
for(int i=1;i<=n;i++)
p[i]=i;
while(m--){
cin>>a>>b;
int pb=find(b),pa=find(a);
if(pa!=pb)
p[pa]=pb;
}
int ans=0;
for(int i=1;i<=n;i++){
if (p[i]==i)ans++;
}
printf("%d\n",ans);
}
#ifdef DEBUG
fclose(stdin);
fclose(stdout);
#endif
return 0;
}