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

Description

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
讲解:使用并查集,每输入一行学生编号数据后,都判断第一个学生编号的祖先是否和后面每一个学生编号的祖先相同,若不同,则将第一个编号的祖先变为该后面编号的祖先,
将集合合并,且每合并一次,第一个编号的祖先上的元素个数都等于合并之前两个祖先上的元素个数之和(最初,每一个编号的祖先都是该编号自身,且对应的元素个数都为一),
最后输出编号为0的祖先上的元素个数即可
总结及出错情况:该题主要就是使用并查集,注意在合并的时候祖先上的元素个数要更新,且最初的时候每一个编号的祖先上的元素个数都是1,最易出错的就是在输入的数据中没有0
就以为没有感染嫌疑者,其实这种情况应该是有一个,因为0在的号代表的学生就是感染嫌疑者
AC代码:
 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 30010
int vis[N],parent[N],m,n;
void begin()
{
for(int i=;i<m;i++)
{
parent[i]=i;
vis[i]=;
}
}
int find (int x)
{
if(parent[x]!=x)
{
parent[x]=find(parent[x]);
}
return parent[x];
}
int query(int x,int y)
{
int px=find(x);
int py=find(y);
if(px!=py)
{
parent[py]=px;
vis[px]=vis[py]+vis[px];//父亲不同的话统计个数,相同的话,不用统计了;
}
}
int main()
{
int first,k,a;
while(cin>>m>>n && m+n)
{
begin();
for(int i=; i<n; i++)
{
scanf("%d %d", &k,&first); // 首先把第一个输入的,当成父亲;
for(int j=; j<k; j++)
{
scanf("%d",&a); //后面的如果有父亲,并且不和第一个父亲相同,
query(first ,a); //则后面的父亲,为第一个的父亲
}
}
printf("%d\n",vis[find()]);
}
return ;
}
 

poj 1611 The Suspects 并查集变形题目的更多相关文章

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

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

  2. POJ 1611 The Suspects (并查集+数组记录子孙个数 )

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

  3. POJ 1611 The Suspects (并查集求数量)

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

  4. POJ 1611 The Suspects 并查集 Union Find

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

  5. poj 1611 The Suspects 并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 30522   Accepted: 14836 De ...

  6. [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 De ...

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

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

  8. poj 1611 :The Suspects经典的并查集题目

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  9. 并查集 (poj 1611 The Suspects)

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

随机推荐

  1. 9.线程通信wait、notify

    线程之间通信 1.线程是操作系统的独立的个体,但这些个体如果不经过特殊处理就不能成为一个整体. 2.使用wait.notify,方法实现线程通信(2个方法都是需要object方法) 3.wait(释放 ...

  2. .net 导出带条码的PDF

    Nuget添加引用:ZXing.Net生成条形码,ZXing.Net.Bindings.ImageSharp生成图片 将图片流插入单元格 举个栗子: BarcodeWriter writer = ne ...

  3. Unity的shader学习2

    下面继续看基于surface的shader代码,基本与Vertex&Fragment shader差不多,只是不能写pass,然后只需要声明surface函数,就能处理所有的事情. Shade ...

  4. datagridview 单元格类型转换【备忘】

    datagridview  在设定列类型后,其下面所有行的该列都与设定的列类型相同. 在需要改变某一行的某个单元格时,遇到了一些问题,再次进行备忘: 之前在遇到该问题时参考别人的博客解决过,但是时间久 ...

  5. iOS: xcode打包上传iTunes失败,iTunes Store operation failed,this action can not complete .try again

    通过xcode点击“upload to app store”上传到itunes,结果一直提示“itunes store operation failed” 原因:网速的问题,我之前也遇到过,网速好的时 ...

  6. C#使用DirectShow播放视频文件 [转]

    原文网址:http://blog.csdn.net/openzpc/article/details/48442751 最近在开发一个视频播放软件,主要要求就是循环播放多个视频文件,并且要求两个视频文件 ...

  7. JavaScript中的递归函数问题

    学过其它编程语言的都应该会知道递归这个问题,递归函数是在一个函数通过名字调用自身的情况下后构成的. function fac(num){ if(num<=1){ return 1; }else{ ...

  8. TensorFlow 入门 上(自用)

    下文会出现的一些知识点:TensorFlow的计算模型.数据模型.运行模型,TensorFlow的工作原理. 两个重要概念——Tensor和Flow: Tensor是张量,在TensorFlow中可以 ...

  9. Elasticsearch入门基础(1)

    基础概念 Elasticsearch有几个核心概念.从一开始理解这些概念会对整个学习过程有莫大的帮助. 接近实时(NRT)         Elasticsearch是一个接近实时的搜索平台.这意味着 ...

  10. rtsp摘要认证协议(Response计算方法)

    rtsp摘要认证协议(Response计算方法) 说明: 例如:OPTIONS rtsp://192.168.123.158:554/11RTSP/1.0   RTSP客户端应该使用username ...