标签:cout cto -- find man ons problem i++ hdu
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
const int maxn=1004;
int n,m;
int set[maxn];
int rank1[maxn];//rank1[i]表示i的深度
int find(int x){
if(x==set[x])
return set[x];
return set[x]=find(set[x]);
}
void merge(int a,int b){
int ta=find(a);
int tb=find(b);
if(ta!=tb){
if(rank1[ta]<rank1[tb]){
set[ta]=tb;
}else{
set[tb]=ta;
if(rank1[ta]==rank1[tb]){
rank1[ta]++;
}
}
}else return;
}
int main(){
int t;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=1;i<=n;i++){
set[i]=i;
rank1[i]=0;
}
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
merge(x,y);
}
int sum=0;
for(int i=1;i<=n;i++){
if(set[i]==i){
sum++;
}
}
cout<<sum<<endl;
}
return 0;
}
标签:cout cto -- find man ons problem i++ hdu
原文地址:https://www.cnblogs.com/GarrettWale/p/11405878.html