The Suspects POJ - 1611

原题链接

考察:并查集基础

在读入学生团体的时候,没必要用数组存储,直接将他们并入树中即可,注意:当我们计算人数的时候,已经在一个集合中的人不能重复计算

当时看y总犯了这个错误,结果自己写又错了233

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int N = 3e4+10;
 5 int p[N],cnt[N];
 6 int findf(int x)
 7 {
 8     if(p[x]!=x) p[x] = findf(p[x]);
 9     return p[x];
10 }
11 int main()
12 {
13 //    freopen("in.txt","r",stdin);
14     int n,m;
15     while(scanf("%d%d",&n,&m)!=EOF&&n)
16     {
17         for(int i=0;i<n;i++) { p[i] = i; cnt[i] = 1; }
18         for(int i=1;i<=m;i++){
19             int k,first,t; scanf("%d%d",&k,&first);
20             for(int j=1;j<=k-1;j++){
21                 scanf("%d",&t);
22                 if(p[findf(t)]!=p[findf(first)]){
23                     cnt[findf(first)]+=cnt[findf(t)];
24                     p[findf(t)] = findf(first);
25                 }
26             }
27         } 
28         printf("%d\n",cnt[findf(0)]);
29     }
30     return 0;
31 }

 

posted @ 2021-01-03 10:14  acmloser  阅读(54)  评论(0编辑  收藏  举报