个人心得:最基础的并查集经典题。借此去了解了一下加深版的即加权并查集,比如食物链的题目,这种题目实行起来还是有

一定的难度,不仅要找出与父节点的关系,还要在路径压缩的时候进行更新,这一点现在还是没那么上手,不过先知道思维

还是好的吧。这道水题就不多提了...就是注意合并的时候以数小的为跟节点就好了

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 file 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
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int person[];
void init()
{
for(int i=;i<n;i++)
person[i]=i;
}
int geta(int x)
{
if(person[x]!=x)
person[x]=geta(person[x]);
return person[x];
}
void mergea(int x,int y)
{
if(geta(x)<geta(y))
person[geta(y)]=geta(x);
else
person[geta(x)]=geta(y);
}
int number(){
int sum=;
for(int i=;i<n;i++)
if(geta(i)==) sum++;
return sum;
}
int main()
{ while(cin>>n>>m)
{
init();
if(!m&&!n) break;
while(m--)
{
int text,x,y;
int flag=;
cin>>text;
while(text--)
{
cin>>x;
if(flag==)
{flag=;y=x;}
else
mergea(y,x);} }
int sum=number();
cout<<sum<<endl; }
return ;
}
												

最新文章

  1. 1. Activiti 运行时表信息总结
  2. [WPF]UserControl的MouseWheel事件触发
  3. 第十五课:奇葩的元素节点iframe
  4. Oracle 应用于.NET平台
  5. 分享Kali Linux 2016.2第48周虚拟机
  6. Oracle 增加修改删除字段与添加注释
  7. mac 下 parallels 虚拟机 ubuntuServer 安装 parallels tools
  8. Javascript里的那些距离们
  9. 怎么修改mysql密码
  10. SPRING IN ACTION 第4版笔记-第一章-003-AOP介绍
  11. UVA 10404 Bachet&#39;s Game(dp + 博弈?)
  12. 多个Tomcat 配置多个JDK
  13. Java 垃圾回收机制(早期版本)
  14. iOS开发经验相关知识
  15. 【XSY1528】azelso 概率&amp;期望DP
  16. idea整合SVN以及SVN的使用
  17. Linux下Mongodb安装和启动配置 原
  18. 开始学习使用 Semantic UI
  19. spring集成redis——主从配置以及哨兵监控
  20. Java反射机制在Spring IOC中的应用

热门文章

  1. mysql利于cte进行分组统计并计算占比
  2. 学会Retrofit+OkHttp+RxAndroid三剑客的使用,让自己紧跟Android潮流的步伐
  3. Windos Server 2008 配置定时清理任务
  4. 20145240 《Java程序设计》第四次实验报告
  5. 大话设计模式之PHP篇 - 观察者模式
  6. 容器rootfs
  7. HMM代码实现
  8. spring boot 使用thymeleaf3.0以及thymeleaf的热部署
  9. Codeforces Round #367 (Div. 2) A , B , C
  10. javascript是一种面向对象语言吗?如果是,您在javascript中是如何实现继承的呢