POJ1611:The Suspects(模板题)
http://poj.org/problem?id=1611
Description
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
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
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(模板题)的更多相关文章
- POJ1611 The Suspects 并查集模板题
题目大意:中文题不多说了 题目思路:将每一个可能患病的人纳入同一个集合,然后遍历查找每个点,如果改点点的根节点和0号学生的根节点相同,则该点可能是病人. 模板题并没有思路上的困难,只不过在遍历时需要额 ...
- [AHOI 2009] 维护序列(线段树模板题)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- POJ2774 & 后缀数组模板题
题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- HDU-3549 最大流模板题
1.HDU-3549 Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- HDU-2222 Keywords Search(AC自动机--模板题)
题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...
- Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover Problem's Link: http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...
- AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- hosts文件配置不生效的解决办法
分析可能的原因并给出相应的解决方案. 第一 种情况,在开启浏览器的时候修改磁盘上的hosts文件,比如说加了原先没有的一句"127.0.0.1 www.360.cn",保存host ...
- golang - channels
如果说goroutine是Go语音程序的并发体的话,那么channels它们之间的通信机制.一个channels是一个通信机制,它可以让一个goroutine通过它给另一个goroutine发送值信息 ...
- zynq里面的AXI总线(2017-1-11)
在ZYNQ中有支持三种AXI总线,拥有三种AXI接口,当然用的都是AXI协议.其中三种AXI总线分别为: AXI4:(For high-performance memory-mapped requir ...
- 分块+莫队||BZOJ3339||BZOJ3585||Luogu4137||Rmq Problem / mex
题面:P4137 Rmq Problem / mex 题解:先莫队排序一波,然后对权值进行分块,找出第一个没有填满的块,直接for一遍找答案. 除了bzoj3339以外,另外两道题Ai范围都是1e9. ...
- 短信文本pdu模式解析
来源于互联网 年代较长 如有侵犯 请联系删除 text模式主要发送字符集(有限的),不能用来发送中文,但是hex moder可以发送所有字符. pdu moder被所有手机支持,主要分为7bit 8b ...
- AngularJS 常用的功能
第一 迭代输出之ng-repeat标签ng-repeat让table ul ol等标签和js里的数组完美结合 例: <ul><li ng-repeat="person in ...
- readonly enable
<input type="text" id="UserName" style="width:20%;" disabled=" ...
- Page4:线性系统的运动求解以及脉冲响应矩阵与传递函数的关系[Linear System Theory]
内容包含线性系统的运动求解,系统矩阵特征值和特征向量对运动的影响,脉冲响应矩阵与传递函数之间的关系
- redis有序集合性能 列表、集合、有序集合
https://www.cnblogs.com/pirlo21/p/7120935.html 1.1 列表 列表(list)类型是用来存储多个字符串,元素从左到右组成一个有序的集合.列表中的每个字符串 ...
- day3_字典
一.说明 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: dict = {key1:value1,key2 ...