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. 利用@keyframe及animation做一个页面Loading时的小动画

    前言 利用@keyframe规则和animation常用属性做一个页面Loading时的小动画. 1  @keyframe规则简介 @keyframes定义关键帧,即动画每一帧执行什么. 要使用关键帧 ...

  2. 编程语言与Python学习(一)

    1.1 编程与编程语言 1.1.1 编程语言 计算机的发明,是为了用机器解放人力,而编程的目的则是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而达到让计算机能够像人脑一样自动执行 ...

  3. 记一次使用 removeEventListener 移除事件监听失败的经历

    测试一 测试代码如下 var Test = function() { this.element = document.body; this.handler = function() { console ...

  4. maven/ssm框架搭建

    好久没有写java了,昨天学了下maven,不用手动的下载和添加jar包,实在是太方便. ------------------------------------------------------- ...

  5. HDU 1171 Big Event in HDU【01背包】

    题意:给出n个物品的价值和数目,将这一堆物品分给A,B,问怎样分使得两者的价值最接近,且A的要多于B 第一次做的时候,没有思路---@_@ 因为需要A,B两者最后的价值尽可能接近,那么就可以将背包的容 ...

  6. windows2008搭建ftp服务器

    控制面板 操作—〉添加角色 (web,文件服务) windows 搜索中: 添加ftp站点: 计算机---管理 –服务器管理  --- 添加一个用户 ,密码. 防护墙高级设置中添加 出入站规则 允许2 ...

  7. 大数相乘(牛客网ac通过)

    2019-05-172019-05-17 大数相乘基本思想: 相乘相加,只不过大于10先不进位到计算完后统一进位 #include <iostream> #include <stri ...

  8. git远程仓库变更

    查看自己的远程仓库 git remote -v 远程仓库变更 git remote remove origin //移出现有的远程仓库的地址 git remote add origin http:// ...

  9. URL正则

    现有需求 表单填写域名只能填写 baseURL 或者 baseURL+端口 不带协议 否则为不合法 String url1 = ".com:90"; String url2 = & ...

  10. jquery 取页面中ifram中得节点

    <iframe src="html/bai.jsp" frameBorder=0 id=middle name=middle scrolling="yes" ...