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

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

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

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. java word导入导出工具类

    package com.shareworx.yjwy.utils; import java.io.InputStream; import java.util.HashMap; import java. ...

  2. git 分支合并处理

    Git 分支 - 分支的新建与合并 https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%85%B3%E4%BA%8E%E7%89%88%E6%9 ...

  3. flask实现模仿知乎

    上个月花了一个月的时间学习flask框架实现了一个简陋的知乎,有提问,回答,写文章,个人信息页,个人信息修改等功能,因刚接触学习flask,后端代码实现很多冗余,依旧需要修改. github: htt ...

  4. 【Head First Servlets and JSP】笔记15:建立一个JSP页面来显示被访问了多少次

    1.这是一个非常简单的程序,它看起来是这个样子的: 实际功能就是,每访问该页面一次count数加1,在服务器重启前(JVM重启前),这个次数将持续累加. 2.因为这个程序过于简单,所以我希望可以通过H ...

  5. P4317 花神的数论题

    题目 洛谷 数学方法学不会%>_<% 做法 爆搜二进制下存在\(i\)位\(1\)的情况,然后快速幂乘起来 My complete code #include<bits/stdc++ ...

  6. php数组函数-array_merge()

    array_merge()函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值.如果数组是数字 索引,则键名会以连续方式重新索引. 注:如果仅仅向array_merg ...

  7. 20145240 《Java程序设计》第五周学习总结

    20145240 <Java程序设计>第五周学习总结 教材学习内容总结 语法与继承结构 8.1.1使用try.catch java中所有的错误都会被打包为对象,并提供了特有的语句进行处理. ...

  8. 完全重装python和yum

    本文原链接 http://smilepad.blog.51cto.com/6094369/1333478 http://blog.etc168.com/?p=642 1.删除现有Python #roo ...

  9. Nginx虚拟主机配置模板

    /////////////////////////////写在前头//////////////////////////////////////////Nginx 服务器中文文档:http://www. ...

  10. 6.0动态加载权限用PermissionGen

    ndroid 6.0 新增加了运行时的动态添加权限,在此介绍一个第三方库,PermissionGen,可以很方便简洁的增加 6.0权限 首先给大家上  PermissionGen 库地址:https: ...