首页 > 其他 > 详细

The Suspects POJ - 1611

时间:2021-01-03 15:11:17      阅读:23      评论:0      收藏:0      [点我收藏+]

原题链接

考察:并查集基础

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

当时看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 }

 

The Suspects POJ - 1611

原文:https://www.cnblogs.com/newblg/p/14224889.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!