http://poj.org/problem?id=1611

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号患了病毒,只要和0号一组的人同样会患病毒。。。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int bin[];
int q[];
int n,m;
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int findx(int x)
{
int r,j,k;
r=x;
while(bin[r]!=r)
r=bin[r];
k=x;
while(k!=r)
{
j=bin[k];
bin[k]=r;
k=j; }
return r;
}
void merge(int x,int y)
{
int fx,fy;
fx=findx(x);
fy=findx(y);
if(fx!=fy)
{
bin[fx]=fy;
}
} int main()
{
int n,x,t,i,j;
while(scanf("%d%d",&n,&x)!=EOF)
{
if(n==&&x==) break;
for(i=; i<n; i++)
bin[i]=i;
while(x--)
{
scanf("%d",&t);
memset(q,,sizeof(q));
for(j=; j<t; j++)
scanf("%d",&q[j]);
qsort(q,t,sizeof(q[]),cmp);
for(j=; j<t; j++)
{
merge(q[j],q[j-]);
} }
/*for(i=0;i<n;i++)
printf("...%d",bin[i]);*/
int l=;
for(i=; i<n; i++)
{
if(findx(i)==findx())
l++; }
printf("%d\n",l); }
return ;
}

POJ1611:The Suspects(模板题)的更多相关文章

  1. POJ1611 The Suspects 并查集模板题

    题目大意:中文题不多说了 题目思路:将每一个可能患病的人纳入同一个集合,然后遍历查找每个点,如果改点点的根节点和0号学生的根节点相同,则该点可能是病人. 模板题并没有思路上的困难,只不过在遍历时需要额 ...

  2. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  3. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  4. POJ2774 & 后缀数组模板题

    题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...

  5. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  6. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  7. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  8. HDU-2222 Keywords Search(AC自动机--模板题)

    题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...

  9. Dancing Link --- 模板题 HUST 1017 - Exact cover

    1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...

  10. AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. 6 CLR实例构造器

    引用类型构造器 如果我们没有定义实例构造器,那么编译器会为我们默认产生一个无参构造器. 实例对象初始化过程 为实例分配内存: 初始化附加成员,包括方法表指针和SyncBlockIndex变量(我们已经 ...

  2. DAX Editor VSIX project

    DAX Editor is a Visual Studio extension that implements a language service for DAX language for SQL ...

  3. Linux目录结构及文件基本操作

    作业: 1.创建一个homework目录,在该目录下新建名为1.txt~10.txt的文件 2.删除1.txt~5.txt 代码: mkdir homework cd homework touch { ...

  4. [No0000132]正确使用密码加盐散列[译]

    如果你是一个 web 开发工程师,可能你已经建立了一个用户账户系统.一个用户账户系统最重要的部分是如何保护密码.用户账户数据库经常被黑,如果你的网站曾经被攻击过,你绝对必须做点什么来保护你的用户的密码 ...

  5. Elasticsearch全文检索实战小结

    一.项目概述 这是一个被我称之为“没有枪.没有炮,硬着头皮自己造”的项目.项目是和其它公司合作的三个核心模块开发. 使用ES的目的是: 1).采集数据.网站数据清洗后存入ES: 2).对外提供精确检索 ...

  6. 解决vshost32.exe已停止工作

    VS2015,搞二次开发遇到这个问题,这个真的很坑,都没法找到问题.然后百度到答案,将调试中的"启用Visual Studio 承载进程"的√去掉: 一开始感觉是内存的问题,后来又 ...

  7. arcengine直连sde

    搞了半天,找了好多资料,实验好多次,终于解决.参考资料:http://www.cnblogs.com/gaxin/p/5777864.html

  8. 解决GP服务产生的结果无法自动发布为地图服务的问题

    在ArcGIS for Javascript API或REST API调用GP服务时,常常会遇到这样一种情况:GP服务运行以后,执行成功,也能够生成结果,然而结果并没有直接产生动态的地图服务供API调 ...

  9. 量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html

    量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html

  10. c#构造函数对string类型赋初值

    public class Stu { public Stu() { //当成员属性非常多难以一一赋值时,采用本方法.当然写代码逐一成员直接赋值效率更高. AssignEmptyStringMember ...