The Suspects

题目传送门~

Time Limit: 2000 msMemory Limit: 65536 KB

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.

In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).

Once a member in a group is a suspect, all members in the group are suspects.

However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n-1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.

A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input

100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0

Sample Output

4
1
1


题后两句话:

①本题是并查集经典问题,三个关键,find函数、merge函数和输入输出的组织。

②这题其实就是并查集的基础框架,一些小细节跟随自己编程习惯调整好就行。

③我是在原有基础上进行了路径压缩和降维合并,然后memset函数呀,输入判断这些小东西就大家自己领悟啦~


话不多说,上才艺!

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;int father[30002];
int n,m; int find(int x){int r=x;while(father[r]>=0) r=father[r];while(x!=r){int ans=father[x];father[x]=r;x=ans;}//路径压缩 return r;
}void merge(int x,int y){//降维合并 int fx,fy;fx=find(x);fy=find(y);int temp=father[fx]+father[fy];if(fx!=fy){if(father[fx]>father[fy]){//由于树根的节点是负值,所以值越大则对应的集合元素越少 father[fy]=temp;father[fx]=fy;}else{father[fx]=temp;father[fy]=fx;}}
} int main()
{while((scanf("%d%d",&n,&m)!=EOF),(n+m)){memset(father,-1,sizeof(father));//初始化 while(m--){int num,a,b;scanf("%d%d",&num,&a);while(--num){scanf("%d",&b);merge(a,b);}           }cout<<-father[find(0)]<<endl;}return 0;
}

ZOJ 1789 The Suspects(经典并查集)相关推荐

  1. The Suspects(并查集)

    The Suspects(并查集) Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiolo ...

  2. POJ 1611 The Suspects (并查集)

    The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...

  3. B - The Suspects(并查集)详解

    题目描述:n个学生分属m个团体,一个学生可以属于多个团体.一个学生疑似患病则它所属的整个团体都疑似患病.已知0号学生疑似患病,以及每个团体都由哪些学生构成,求一共多少个学生疑似患病. 解题思路:本题的 ...

  4. POJ 1611 The Suspects (并查集)

    文章作者:ktyanny 文章来源:ktyanny 转载请注明,谢谢合作. ktyanny:a的第一道并查集. 题目描述: 有很多组学生,在同一个组的学生经常会接触,也会有新的同学的加入.但是SARS ...

  5. hdu 1232 经典并查集应用

    http://acm.hdu.edu.cn/showproblem.php?pid=1232 完全就是并查集的应用啊... View Code 1 #include<iostream> 2 ...

  6. The Suspects(并查集入门)

    题目:http://www.fjutacm.com/Problem.jsp?pid=2021 题意大概就是输入n,m,分别代表总共n个人,m组,每组输入k,后面再输入k个人表示是一组的,0号是嫌疑者, ...

  7. POJ 1611 -The Suspects (并查集)

    题目 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, ...

  8. zoj 1789 The Suspects

    好高兴,又AC一道 ,不过是很类似的两道..还是好高兴呀思想跟2833是一样的,不过要重新设计输入和输出.老师上课又重新讲解了一下,因为嫌疑人已知是0,所以加入集中时应该默认让数值小的做树根,即最终让 ...

  9. 1114 Family Property (25分) (并查集) 复杂题 经典并查集

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  10. 并查集c++代码_[Leetcode 每日精选](本周主题-并查集) 547. 朋友圈

    题目难度: 中等 原题链接 今天继续来做并查集的问题, 这道题仍然比较基础, 而且也是个比较接近现实的问题了. 大家在我的公众号"每日精选算法题"中的聊天框中回复 并查集 就能看到 ...

最新文章

  1. pandas使用bdate_range函数获取起始时间(start)和结束时间(end)范围内的所有周末日期(weekends day)
  2. 三本毕业后,我进入了世界五百强
  3. IOS 6.0+ Autolayout — UITableViewCell 高度调整
  4. 推荐系统笔记:Introduction
  5. 【算法与数据结构】中缀表达式转为后缀表达式
  6. JavaWeb(六)——HttpServletResponse、HttpServletRequest
  7. 征信一个月查40次,还能贷款吗?
  8. random()模块随机函数的用法总结
  9. c语言图像函数怎么用,请教 怎么才能用C输出一个函数的图像?大侠 帮帮忙啊...
  10. Hyper-V常用问题解惑
  11. mysql image类型_MyCat教程【mysql主从复制实现】
  12. 【基础练习】【区间DP】codevs1090 加分二叉树题解
  13. php7 fastdfs,关于centos7 fastdfs部署
  14. 二阶矩阵转置怎么求_这个二阶矩阵的二范数怎么求
  15. 倒排索引、正排索引,以及ElasticSearch对倒排索引的优化方法
  16. PMP考试 工作绩效数据 工作绩效信息 工作绩效报告 区别与联系
  17. LTE终端分类-LTE UE category
  18. 小白也能看懂的零信任SDP介绍
  19. 为什么敏捷开发难于成功?
  20. 用python进行按掩膜提取的批量操作

热门文章

  1. 计网实验总结一:路由器配置
  2. MQTT客户端软件(MQTT.fx)的使用详解
  3. 中台战略-第五章、中台建设方法论
  4. linux的OOM killer
  5. Ceres的Options详解
  6. 用计算机弹平凡之路谱子,pen beat曲谱_penbeat平凡之路的谱子
  7. 2017计算机知识竞赛题,2017年《西游记》知识竞赛试题100题附答案.doc
  8. 游戏史上最伟大的10位制作人(图)
  9. 怎样使用secureCRT连接路由器
  10. GIF动态表情图如何制作