标签:memset http problem std ref iostream || else href
疫情严重,在家玩题,结果第一个就是关于SARS的(大雾...)
简单的并查集
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn= 3e4+3;
const int maxm= 5e2+3;
int pa[maxn];
int ans;
int rnk[maxn];
int Find(int x)
{
if (x== pa[x]){
return x;
}
return pa[x]= Find(pa[x]);
}
void Merge(int a, int b)
{
a= Find(a);
b= Find(b);
if (a== b){
return;
}
if (rnk[a]< rnk[b]){
pa[a]= b;
}
else{
pa[b]= a;
if (rnk[a]== rnk[b]){
++rnk[a];
}
}
}
void Init(int n)
{
memset(rnk, 0, sizeof(rnk));
for (int i= 0; i< n; ++i){
pa[i]= i;
}
rnk[0]= maxm;
ans= 0;
}
int main()
{
int n, m, k, x, y;
while (EOF!= scanf("%d %d", &n, &m) && (n || m)){
Init(n);
for (int i= 0; i< m; ++i){
scanf("%d", &k);
scanf("%d", &x);
for (int j= 1; j< k; ++j){
scanf("%d", &y);
Merge(x, y);
}
}
for (int i= 0; i< n; ++i){
if (0== Find(i)){
++ans;
}
}
printf("%d\n", ans);
}
return 0;
}
标签:memset http problem std ref iostream || else href
原文地址:https://www.cnblogs.com/Idi0t-N3/p/12236358.html