标签:als oid net == ace for vector name find
这题就是并查集的简单应用
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
const int maxn=30004;
int n,m;
int set[maxn];
int rank1[maxn];//rank1[i]表示i的深度
map<int,int> stu;
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;
}
vector<int> group[505];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>n>>m&&(n||m)){
stu.clear();
for(int i=0;i<n;i++){
set[i]=i;
rank1[i]=0;
}
int k;
for(int i=0;i<m;i++){
group[i].clear();
cin>>k;
int id;
int pre;
cin>>pre;
for(int j=1;j<k;j++){
cin>>id;
merge(id,pre);
}
}
int t=find(0);
int sum=0;
for(int i=0;i<n;i++){
if(t==find(i))
sum++;
}
cout<<sum<<endl;
}
return 0;
}
标签:als oid net == ace for vector name find
原文地址:https://www.cnblogs.com/GarrettWale/p/11405852.html