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

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
讲解:使用并查集,每输入一行学生编号数据后,都判断第一个学生编号的祖先是否和后面每一个学生编号的祖先相同,若不同,则将第一个编号的祖先变为该后面编号的祖先,
将集合合并,且每合并一次,第一个编号的祖先上的元素个数都等于合并之前两个祖先上的元素个数之和(最初,每一个编号的祖先都是该编号自身,且对应的元素个数都为一),
最后输出编号为0的祖先上的元素个数即可
总结及出错情况:该题主要就是使用并查集,注意在合并的时候祖先上的元素个数要更新,且最初的时候每一个编号的祖先上的元素个数都是1,最易出错的就是在输入的数据中没有0
就以为没有感染嫌疑者,其实这种情况应该是有一个,因为0在的号代表的学生就是感染嫌疑者
AC代码:
 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 30010
int vis[N],parent[N],m,n;
void begin()
{
for(int i=;i<m;i++)
{
parent[i]=i;
vis[i]=;
}
}
int find (int x)
{
if(parent[x]!=x)
{
parent[x]=find(parent[x]);
}
return parent[x];
}
int query(int x,int y)
{
int px=find(x);
int py=find(y);
if(px!=py)
{
parent[py]=px;
vis[px]=vis[py]+vis[px];//父亲不同的话统计个数,相同的话,不用统计了;
}
}
int main()
{
int first,k,a;
while(cin>>m>>n && m+n)
{
begin();
for(int i=; i<n; i++)
{
scanf("%d %d", &k,&first); // 首先把第一个输入的,当成父亲;
for(int j=; j<k; j++)
{
scanf("%d",&a); //后面的如果有父亲,并且不和第一个父亲相同,
query(first ,a); //则后面的父亲,为第一个的父亲
}
}
printf("%d\n",vis[find()]);
}
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: 24134   Accepted: 11787 De ...

  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 1611:The Suspects(并查集,经典题)

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

  8. poj 1611 :The Suspects经典的并查集题目

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  9. 并查集 (poj 1611 The Suspects)

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

随机推荐

  1. nose的setup和teardown

    参考:http://blog.csdn.net/linda1000/article/details/8533349 1.模块的setUp和tearDown def setUp(): print &qu ...

  2. Nescafé2 月之谜 题解

    月之谜 (mystery.pas/c/cpp) [题目描述] 打败了 Lord lsp 之后,由于 lqr 是一个心地善良的女孩子,她想净化 Lord lsp 黑化的心,使他变回到原来那个天然呆的 l ...

  3. STL iterator和reverse_iterator

    先看一段代码: #include <iostream> #include <deque> #include <algorithm> #include <ite ...

  4. 【转载】秒杀场景下MySQL的低效原因和改进以及Redis的处理

    分享的PPT在如下网址: http://www.doc88.com/p-4199037770087.html 秒杀场景下mysql的低效原因和改进 另外有一个篇文章是针对以上内容的总结: http:/ ...

  5. Linux下读取RFID卡号(C串口编程)

    由于项目需要用到RFID.GPRS.摄像头等模块所以便看了一下,整理了一下学习思路,本篇先是整理一下串口读取RFID卡号的程序思路,后面还会更其他的 RFID模块: 本次采用的是125K的RFID读卡 ...

  6. anisotropic filter

    各项异性 point一次sample 1 bilinear 一次sample 4 trilinear 一次sample 8   两个4按mipmaplevel混合 anisotropic 异性和同性 ...

  7. TextView子类的常用属性

    TextView常见的子类包括EditText,Button,CheckBox, RadioButton等. 1.EditText EditText继承自TextView,因此TextView所有属性 ...

  8. Ext.encode 与 Ext.decode .

    Ext.encode( Mixed o ) : String: json对象转换json字符串 Ext.decode( String json ) : Object: json字符串转换json对象 ...

  9. mybatis模糊查询语句

    articletitle like concat('%',#{articletitle},'%')

  10. js 替换json对象中的键名

      js 替换json对象中的键名 CreateTime--2018年3月30日15:38:50 Author:Marydon 情景描述: 有个json数组,现在需要将json对象中的key替换掉,值 ...