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

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

Source

 
题目及算法分析:有n个人,编号0---n-1,m个组,输入这m组每组人的编号,每个组都是一个小集体。
     假设0号人是嫌疑人,所有跟嫌疑人在一个组的都是嫌疑人。注意:如果1和0在某个组,并且1和2, 3, 4等在一个组,这些人也都是嫌疑人!
采用并查集算法进行合并计算算法,需要开辟一个数组进行0号,进行每个节点的子孙数目的统计保存。
代码:
 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#define N 30000 using namespace std;
int n, m;
int cnt[N];
int fa[N]; //0 < n <= 30000 and 0 <= m <=500
void init()
{
for(int i=; i<N; i++)
{
fa[i]=i;
cnt[i]=;
}
} int findset(int x)
{
return fa[x]!=x? fa[x]=findset(fa[x]):x;
} void union_set(int x, int y)
{
int xx=findset(x);
int yy=findset(y);
if(xx==yy) return; //说明两元素本来就属于同一个集合 返回
else if(xx<yy) //如果x的根节点比y的根节点 小
{
fa[yy]=xx;
cnt[xx]=cnt[xx]+cnt[yy];
}
else if(xx>yy)
{
fa[xx]=yy;
cnt[yy]=cnt[yy]+cnt[xx];
}
} int main()
{
int dd, a, b;
while(scanf("%d %d", &n, &m)!=EOF)
{
if(n== && m==) break;
init();
for(int i=; i<m; i++)
{
scanf("%d", &dd);
scanf("%d", &a);
for(int j=; j<dd-; j++)
{
scanf("%d", &b);
union_set(a, b);
a=b;
}
}
printf("%d\n", cnt[] );
}
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: 20596   Accepted: 9998 D ...

  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-1703 Find them, Catch them(并查集&数组记录状态)

    题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...

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

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

  9. 并查集 (poj 1611 The Suspects)

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

随机推荐

  1. 浅谈APP的分享功能,有时候社交裂变形式比内容更“重要”

    回顾2018年的移动互联网,“社交裂变”“下沉”等成为年度关键词.一方面我们可以看到社交裂变助推用户增长,另一方面我们也看到了以拼多多.趣头条为代表的互联网企业对于社交裂变模式表现出的空前关注度.作为 ...

  2. jenkins按版本发布maven项目

    1.先为java这个项目创建三个版本 vim pom.xml vim src/main/java/com/ghz/testweb/App.java git add . git commit -m &q ...

  3. mybatis ----SqlSessionManager

    今天我们来看看这个类 有些写法还是很经典的 public class SqlSessionManager implements SqlSessionFactory, SqlSession { priv ...

  4. hadoop之hdfs------------------FileSystem及其源码分析

    FileSystem及其源码分析 FileSystem这个抽象类提供了丰富的方法用于对文件系统的操作,包括上传.下载.删除.创建等.这里多说的文件系统通常指的是HDFS(DistributedFile ...

  5. javascript --- 原型初探七日谈(二)

    扩展内建对象: 在javascript中,内建对象的构造函数都是可以通过其原型来进行扩展的.这意味着我们可以做一些事情,例如我们要往数组原型中添加一个新方法,就可以在其所有的数组中使用,下面我们来试试 ...

  6. go 依赖包管理工具gb安装报错

    尝试了下gb工具,发现有个问题: [root@etcd1 test]# go get github.com/constabulary/gb/... /home/gopath/src/github.co ...

  7. Android GC 原理探究

    导语 想写一篇关于 android GC 的想法来源于追查一个魅族手机图片滑动卡顿问题,由于不断的 GC 导致的丢帧卡顿的问题让我们想了很多方案去解决,所以就打算详细的看看内存分配和 GC 的原理,为 ...

  8. ShadowMap渲染阴影方法及问题 【转】

    ShadowMap基于的原理:SM算法是一个2-pass绘制算法,第一pass从光源视点绘制场景,生成SM纹理,第2pass从视点视图按常规方法绘制场景 从光源的位置观察场景,这时候我们看不到的地方就 ...

  9. PS 抠图如何使用通道法处理头发

      通道抠图法抠出美女飘逸头发-PS抠图实例教程 抠图更换背景后效果图 通道抠图法抠出美女飘逸头发-PS抠图实例教程 教程步骤: 1  打开原图,进入通道面板. 通道抠图法抠出美女飘逸头发-PS抠图实 ...

  10. Odoo MRP 实际成本

    Odoo MRP 8 对于 产成品并不支持 实际成本记账 本人开发了一个模块,支持此特性, 可以在 淘宝店铺 购买 https://item.taobao.com/item.htm?_u=85jr9d ...