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

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

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

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. 'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.

    let newStr = String(str[..<index]) // = str.substring(to: index) In Swift 3 let newStr = String(s ...

  2. 获取文件的MD5值,比较两个文件是否完全相同

    代码: public class MD5Test { public static void main(String[] args) { String s1 = MD5Test.MD5Operation ...

  3. 06_Hadoop配置伪分布式模式详解

    查看IP地址,设为手动模式: 配置hadoop用户sudo权限 su切换到root身份,配置vim /etc/sudoers文件,加入 hadoop ALL=(root)NOPASSWD:ALL    ...

  4. css小技巧1

    资料 1. 文本省略 单行省略: white-space:nowrap; overflow:hidden; text-overflow:ellipsis; 多行文本省略: 只兼容webkit内核,不属 ...

  5. 0625 Django 基础

    相关命令: 1 创建项目 django-admin startproject 项目名称 2 创建应用 python manage.py startapp app名称 3 启动项目 python man ...

  6. 20165101刘天野 2017-2018-2 《Java程序设计》第6周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容总结 第八章:常用实用类 String类:不可变类,一些看起来能够改变String的方法其 ...

  7. ASP.NET5 MVC6 利用Middleware 创建可访问HttpContext 的业务类工厂。(代替HttpContext.Current)

    我们的目标是在后台业务处理类中,能够很容易的取得用户信息或者其它HTTP请求相关的信息. 所以,首先我们需要一个存储这些信息的类: public class RequestData { public ...

  8. Linux 系统密码破解

    (一)CentOS Linux 系统密码破解 1.在grub选项菜单按E进入编辑模式 2.编辑kernel那行 /init 1 (或/single) 3.按B重启 4.进入后执行下列命令 root@# ...

  9. Lucene简单介绍

    [2016.6.11]以前写的笔记,拿出来放到博客里面~ 相关软件: Solr, IK Analyzer, Luke, Nutch;Tomcat; 1.是什么: Lucene是apache软件基金会j ...

  10. SpringBoot2.0之整合Kafka

    maven依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...