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. Codeforces 599E Sandy and Nuts(状压DP)

    题目链接 Sandy and Nuts 题意大概就是给出限制条件求出在该限制条件下树的种数. #include <bits/stdc++.h> using namespace std; # ...

  2. 洛谷——P1216 [USACO1.5]数字三角形 Number Triangles

    P1216 [USACO1.5]数字三角形 Number Triangles 题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左 ...

  3. 仓鼠找sugar(lca)

    洛谷——P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅( ...

  4. 被动路由跟踪工具InTrace

    被动路由跟踪工具InTrace   InTrace是一款类似于Traceroute的路由跟踪工具.但它不同的是,他不主动发送数据包,而是通过监听当前主机和目标主机的数据包,进行分析,从而获取路由信息. ...

  5. [java基础] 001 - 记一次堆栈溢出异常(StackOverFlowError)

    上午经理发来一个任务,解决某个接口异常,此接口第一次调用成功返回: {ret=Y, orderResultList=[{itemno=31920190521083622032, sub_msg=成功, ...

  6. Linux下查看某个命令的参数

    1.一般每个命令都带有help参数,使用方法如下: shutdown --help 提示:shutdown为关机命令,在真实环境使用时需要root权限,比如前面加sudo. 2.使用man命令查看,使 ...

  7. 邁向IT專家成功之路的三十則鐵律 鐵律十四:IT人言談之道-守中

    人與人之間的互動交談最重要的莫過於真誠與頃聽.老子曾在所著的道德經之中,言道:「多言數窮,不如守中」,其意思簡單來說就是不要說多餘的話.人與人之間的相處,常為了一個面子問題,你來我往的爭論不休,其實真 ...

  8. 【Exception】查看异常出现在具体的文件名/类名/方法名/具体行号

    今天在处理异常日志保存过程中,想要获取到异常抛出在具体在那个文件,哪个类下的哪个方法中的具体第几行,所以具体实现如下 try{ Integer adminID = Integer.parseInt(a ...

  9. 编写自己的UDTF

    1. UDTF介绍 UDTF(User-Defined Table-Generating Functions) 用来解决 输入一行输出多行(On-to-many maping) 的需求. 2. 编写自 ...

  10. 定义自己的代码风格CheckStyle简单使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-/ ...