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
Source

提供两个模板:

模板一:

 int ufs[MAXN];    //并查集

 void Init(int n)    //初始化
{
int i;
for(i=;i<n;i++){
ufs[i] = i;
}
} int GetRoot(int a) //获得a的根节点。路径压缩
{
if(ufs[a]!=a){ //没找到根节点
ufs[a] = GetRoot(ufs[a]);
}
return ufs[a];
} void Merge(int a,int b) //合并a和b的集合
{
ufs[GetRoot(b)] = GetRoot(a);
} bool Query(int a,int b) //查询a和b是否在同一集合
{
return GetRoot(a)==GetRoot(b);
}

模板二:

 int pre[];
int find(int x){//查找x父节点
int r=x; //委托r去找父节点
while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 )
r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
return r;//父节点驾到~~~
}
void join(int x,int y){ //我想让x节点和节点连成一条线
int fx=find(x),fy=find(y);//寻找x,y的父节点
if(fx!=fy)//x和y的父节点显然不是同一个
pre[fx]=fy;//让x的父节点成为y的子节点
}

题意:
  有n个学生属于m个团体,(0<n<=30000,0<=m<=500)一个学生可以属于多个团体。一个学生疑似患病,则他属于整个团体都疑似患病,已知0号学生疑似患病,求一共多少个学生患病。
思路:
  很经典的并查集的题目,找一个pre[]数组记录存储每一个以当前下标为根节点的集合的个体数目,最后输出0号的根节点对应的sum值,就是0号学生所在团体的人数。
  代码:

 #include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
using namespace std;
int pre[];
int find(int x){//查找x父节点
int r=x; //委托r去找父节点
while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 )
r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
return r;//父节点驾到~~~
}
void join(int x,int y){ //我想让x节点和节点连成一条线
int fx=find(x),fy=find(y);//寻找x,y的父节点
if(fx!=fy)//x和y的父节点显然不是同一个
pre[fx]=fy;//让x的父节点成为y的字节点
}
int main(){
std::ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m){
memset(pre,,sizeof(pre));
if(n==&&m==)break;
for(int i=;i<n;i++) pre[i] = i;
while(m--){
int t, a,b;
cin>>t>>a;
for(int i=;i<t;i++){
cin>>b;
join(a,b);
a=b;
}
}
int sum=;
for(int i=;i<n;i++){
if(find(i)==find())
sum++;
}
cout<<sum+<<endl;
}
return ;
}

poj 1611 :The Suspects经典的并查集题目的更多相关文章

  1. POJ 1611 The Suspects(简单并查集)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> using namespace std; ]; void makeSet(int ...

  2. 【POJ】The Suspects(裸并查集)

    并查集的模板题,为了避免麻烦,合并的时候根节点大的合并到小的结点. #include<cstdio> #include<algorithm> using namespace s ...

  3. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

  4. 并查集 (poj 1611 The Suspects)

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

  5. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  6. POJ 1611 The Suspects (并查集)

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

  7. poj 1611 The Suspects(并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21598   Accepted: 10461 De ...

  8. poj 1611 The Suspects(并查集输出集合个数)

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

  9. poj 1611 The Suspects 并查集变形题目

    The Suspects   Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 20596   Accepted: 9998 D ...

随机推荐

  1. 1级搭建类105-Oracle 19c 单实例 FS(19.3+RHEL 8)公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  2. 在Scala中免费验证

    优锐课带你详细了解如何在Scala中实施免费的monad验证.抽丝剥茧,细说架构那些事! 由于业务数据的复杂性,已经在数据验证上花费了很多精力.在Scala中,提出了使用应用程序进行验证的方法,并被广 ...

  3. flex布局 居中

    display:flex;justify-content: center;检查侧轴是否居中,比如古代竖着写字,检查字是否在每条竹简的中央. display:flex;align-items: cent ...

  4. Hadoop学习之路(5)Mapreduce程序完成wordcount

    程序使用的测试文本数据: Dear River Dear River Bear Spark Car Dear Car Bear Car Dear Car River Car Spark Spark D ...

  5. 【python&pycharm的安装使用】

    一.Python3.7安装 1. 运行python3.7.exe 2. 检查是否安装成功:命令窗口输入python -V 二.Pycharm安装 1. 运行pycharm.exe(社区版) 2. 配置 ...

  6. K3/Cloud 用插件打开一张已存在的单据

    BillShowParameter billpara = new BillShowParameter();billpara.FormId = "SAL_SaleOrder";//单 ...

  7. 论文阅读笔记(四)【TIP2017】:Video-Based Pedestrian Re-Identification by Adaptive Spatio-Temporal Appearance Model

    Introduction (1)背景知识: ① 人脸识别是具有高可靠性的生物识别技术,但在低解析度(resolution)和姿态变化下效果很差. ② 步态(gait)是全身行为的生物识别特征,大部分步 ...

  8. centos7在命令界面使用命令可以执行,但在jenkins中输入命令报Chrome has crashed.

    问题:selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: ex ...

  9. [CF1304E] 1-Trees and Queries - LCA

    由于可以走重边,所以任意一条路径长 + 2 仍然对应至少一条合法路径 很显然我们有 \(3\) 种基本路径 不经过 \((x,y)\) 经过 \(x \to y\) 经过 \(y \to x\) 假设 ...

  10. nginx 启动报错找不到nginx.pid文件

    这个问题的出现应该是系统找不到nginx的配置文件nginx.conf,所以,我们要告诉系统配置文件的位置:' --- 使用nginx -c /usr/local/nginx/conf/nginx.c ...