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 题目大意:
为了防止非典的进一步传播,要你写一个程序,通过已知的学生群体中的患者,找出所有患者,所有的学生都是怀疑对象。
每个案例,第一行 n(学生数量) m(表示有m组数据),接下来便是m组数据,每一组数据表示同组人的个数及其编号,同组的人才能被传染,0为传染源。
output:最多能被传染的人数、 方法:并查集
AC代码:
#include<iostream>
#define maxn 30005
using namespace std;
int p[maxn];
int jishu[maxn];
int fa;
int findx(int x)
{
int temp=x;
return x==p[x]?x,p[temp]=x:findx(p[x]);
}
int myunion(int son, int fa){
int s=findx(son),f=findx(fa);
if(s!=f){
p[s]=f;
jishu[f]+=jishu[s];
}
return ;
}
int main ()
{
int n,m,t;
while(cin>>n>>m&&(n+m)!=)
{
for(int i=;i<=n;i++)
{
p[i]=i;
jishu[i]=;
}
while(m--)
{
int num;
cin>>t;
for(int i=;i<t;i++)
{
cin>>num;
if(i) myunion(num,fa);
fa=num;
}
}
cout<<jishu[findx()]<<endl;;
}
}

这个真的很难理解!

首先

100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
将每一组数据写成一棵树,若有交叉出现,便合并成一棵树
即:用一个数组,先初始化为a【i】=i,然后将同一组数用父子关系联系起来,即 a【son】=father,形成一棵单独的树。
例如:第一组把1作为根,即father,即a【1】=1不变,a【2】=1,即 a【son】=father,第二,三组以此类推。
但是要注意的是,当出现已经有过父子关系的节点时,要分情况讨论。
例如:第一组的1,已经作为2的father,第三组又出现1,作为0的son.这个时候我们不能单纯的写成两棵独立的树,便要合并union。
方法:判断并合并
int myunion(int son, int fa){
int s=findx(son),f=findx(fa); //找到祖宗
if(s!=f){ //判断祖宗是否一致,一致根本就不用管
p[s]=f; //不一致便合并,串起来
jishu[f]+=jishu[s];
}
return ;
}

例如:

第一组之后:p【1】=1,p【2】=1

第三组之后:p【0】=0,p【1】=0,p【2】=0(调用了findx函数,进行了路径压缩)

第四组之后:p【0】=99,p【2】=0,p【99】=99


并查集 (Union Find ) P - The Suspects的更多相关文章

  1. POJ 1611 The Suspects 并查集 Union Find

    本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...

  2. 并查集(Union/Find)模板及详解

    概念: 并查集是一种非常精巧而实用的数据结构,它主要用于处理一些不相交集合的合并问题.一些常见的用途有求连通子图.求最小生成树的Kruskal 算法和求最近公共祖先等. 操作: 并查集的基本操作有两个 ...

  3. 并查集 (poj 1611 The Suspects)

    原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...

  4. 并查集模板题(The Suspects )HZNU寒假集训

    The Suspects Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 36817 Accepted: 17860 Descrip ...

  5. Java 并查集Union Find

    对于一组数据,主要支持两种动作: union isConnected public interface UF { int getSize(); boolean isConnected(int p,in ...

  6. 【裸的并查集】POJ 1611 The Suspects

    http://poj.org/problem?id=1611 [Accepted] #include<iostream> #include<cstdio> #include&l ...

  7. 最小生成树(Minimum Spanning Tree)——Prim算法与Kruskal算法+并查集

    最小生成树——Minimum Spanning Tree,是图论中比较重要的模型,通常用于解决实际生活中的路径代价最小一类的问题.我们首先用通俗的语言解释它的定义: 对于有n个节点的有权无向连通图,寻 ...

  8. bzoj1854 [Scoi2010]游戏【构图 并查集】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1854 没想到怎么做真是不应该,看到每个武器都有两个属性,应该要想到连边构图的!太不应该了! ...

  9. [leetcode] 并查集(Ⅰ)

    预备知识 并查集 (Union Set) 一种常见的应用是计算一个图中连通分量的个数.比如: a e / \ | b c f | | d g 上图的连通分量的个数为 2 . 并查集的主要思想是在每个连 ...

随机推荐

  1. Medium上的文章

    Welcome to Medium, a place to read, write, and interact with the stories that matter most to you. 网站 ...

  2. js中callback执行

    <!DOCTYPE HTML> <html> <head> <meta charset="GBK" /> <title> ...

  3. 使用wpa_supplicant连接WIFI

    让树莓派可以开机就连接制定的wifi, 可以通过wpa_supplicant来实现. 在 /etc/wpa_supplicant 下写一个配置文件: wpa_supplicant.conf 内容如下: ...

  4. Codeforces 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

  5. (转载)Android滑动冲突的完美解决

    Android滑动冲突的完美解决 作者:softwindy_brother 字体:[增加 减小] 类型:转载 时间:2017-01-24我要评论 这篇文章主要为大家详细介绍了Android滑动冲突的完 ...

  6. HDU 1556 Color the ball【树状数组】

    题意:给出n个区间,每次给这个区间里面的数加1,询问单点的值 一维的区间更新,单点查询,还是那篇论文里面讲了的 #include<iostream> #include<cstdio& ...

  7. 关于CAS操作

    在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能问题. (2 ...

  8. [洛谷P1892][codevs2597]团伙

    题目大意:有n个强盗,他们有这样的关系:1.朋友的朋友是朋友:2.敌人的敌人是朋友. 两个人是朋友,则他们在一个团伙中,是敌人则在不同团伙中. 现在给出一些朋友或敌人的关系,问最多有多少团伙.输入保证 ...

  9. C++ STL rope介绍----可持久化平衡树

    大致介绍: rope这个东西,我刚刚知道这玩意,用的不是很多,做个简单的介绍. 官方说明:我是刘邦(我估计你是看不懂的). rope就是一个用可持久化平衡树实现的“重型”string(然而它也可以保存 ...

  10. [LeetCode] 455. 分发饼干 assign-cookies(贪心算法)

    思路: 尽量先将小饼干分配给胃口小的孩子,故而饼干和孩子胃口都应该先排序. python中,a.sort()只能用于a为list, sort()是可变对象的方法,无参数,无返回值,但会影响改变对象. ...