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

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

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

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 ;
}
												

The Suspects (并查集)的更多相关文章

  1. The Suspects(并查集维护根节点信息)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 37090   Accepted: 17980 De ...

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

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

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

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

  4. B - The Suspects(并查集)

    B - The Suspects Time Limit:1000MS     Memory Limit:20000KB     64bit IO Format:%lld & %llu Desc ...

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

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

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

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

  7. POJ 1611 The Suspects 并查集 Union Find

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

  8. poj 1611 The Suspects 并查集

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

  9. The Suspects(并查集求节点数)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 28164   Accepted: 13718 De ...

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

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

随机推荐

  1. 流量分析系统---flume(测试flume+kafka)

    1.在flume官方网站下载最新的flume     wget http://124.205.69.169/files/A1540000011ED5DB/mirror.bit.edu.cn/apach ...

  2. PHP生成缩略图,控制图片质量,支持.png .jpg .gif

    namespace common\components; class ResizeImageHelper { public $type;//图片类型 public $width;//实际宽度 publ ...

  3. 【Flask】Sqlalchemy join

    ### join:1. join分为left join(左外连接)和right join(右外连接)以及内连接(等值连接).2. 参考的网页:http://www.jb51.net/article/1 ...

  4. 逐行读取txt文件并存入到数组中

    get_file_contents_on_line.php $file = fopen("log.txt", "r"); $user=array(); $i=0 ...

  5. accept= 'image/*'反映缓慢

    input[type='file']的accept属性用来指定上传文件的MIME类型. 将其设为accept= 'image/*',顾名思义,过滤掉所有非图片文件, 但在实际操作中,发现有时会出现响应 ...

  6. Python字符串格式转换

    转换类型 转换类型 说明 d, i 带符号十进制 b 无符号二进制 o 无符号八进制 u 无符号十进制 x 无符号十六进制(小写) X 无符号十六进制(大写) e 科学计数法表示的浮点数(小写) E ...

  7. php数组函数-array_diff()

    array_diff()函数返回两个数组的差集数组.该数组包括了所有在被比较数组中,但是不在任何其他参数数组中的键值在返回数组中,键名保持不变. array_diff(array1,array2,ar ...

  8. Web项目java.lang.OutOfMemoryError: PermGen space异常解决

    接手一个新的Web项目,编译运行(Tomcat版本为7),运行的时候报出了java.lang.OutOfMemoryError: PermGen space的异常,搜了一下这样解释:   PermGe ...

  9. Linux VPS实用简单安全配置

    今天,和大家一起来分享VPS最基本的安全配置. 第一.修改SSH端口 VPS默认的SSH端口是22,那些扫描穷举密码的,也势必从22开始,所以,修改22为一个其他的数字,是非常有必要的. 好了,SSH ...

  10. mysql慢查询设置

    不同版本的mysql命令和配置不一样,以下是2个版本 修改配置文件 log-slow-queries=/alidata/mysql-log/mysql-slow.log long_query_time ...