解题思路:一共给出 n个人,m组,接下来是m组数据,每一组开头是该组共有的人 num,则接下来输入的num个数,这些数是一组的

又因为最开始只有编号为0的人携带有病毒,且只有同一组的人会相互传染,问最后一共有多少人携带有病毒,则问题简化为求含有0那一组一共有多少个人。

反思:最开始把对应每输入一组的数据用数组来放,发现 int a[30001]编译不过;

然后就用一个while循环来输入,还有最开始得不到正确结果是因为把最后的判断条件写成了

if(find(i)==0)

sum++;

当时以为含有0的组的根节点就是0,其实0的根节点也不一定就是0,还应该再找一下0的根节点,应该改成

if(find(i)==find(0))

sum++;

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
#include<stdio.h>
int pre[30005];
int find(int root)
{
if(root!=pre[root])
{
pre[root]=find(pre[root]);
}
return pre[root];
}
void unionroot(int x,int y)
{
int root1,root2;
root1=find(x);
root2=find(y);
if(root1!=root2)
{
pre[root1]=root2;
}
} int main()
{
int n,m,num,k,t,i,p;
int x,y;
int sum;
while(scanf("%d %d",&n,&m)!=EOF&&(n||m))
{
sum=0;
for(i=0;i<n;i++)
pre[i]=i;
while(m--)
{
scanf("%d",&num);
scanf("%d",&x);
k=x;
if(num==1)
{
pre[x]=find(x);
}
else
{
p=num-1;
while(p--)
{
scanf("%d",&y);
t=y;
unionroot(k,y);
k=t;
}
}
} for(i=0;i<n;i++)
{
if(find(i)==find(0))
sum++; }
printf("%d\n",sum);
}
}

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 (并查集+数组记录子孙个数 )

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

  4. POJ 1611 The Suspects (并查集求数量)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  5. POJ 1611 The Suspects 并查集 Union Find

    本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...

  6. poj 1611 The Suspects 并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 30522   Accepted: 14836 De ...

  7. [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 De ...

  8. 并查集 (poj 1611 The Suspects)

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

  9. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

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

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

随机推荐

  1. gradle springboot打包时忽略某个配置文件

    jar { exclude "**/bootstrap.properties" }

  2. [SDOI2016]生成魔咒(后缀自动机)

    看一眼题.本质不同的字串数. 嘴角微微上扬. 每一次加一个数输出一个答案. 笑容渐渐消失. 等等,\(SAM\)好像也可以求本质不同的字串. 设当前字符串用\(x\)表示,每次插入完成后\(ans\) ...

  3. Mysql 忘记数据库密码

    windows下忘记MySQL密码的修改方法 1.关闭正在运行的Mysql服务: A.命令下运行   net stop mysql B.找到mysql服务停止mysql的服务 C.在任务管理器中结束M ...

  4. 基于mybatis的CRUD

    u  基于Mybatis的CRUD u  掌握MyBatis的结果类型-resultMap和resultType u  掌握MyBatis的参数类型 u  掌握#和$两种语法 1      基于myb ...

  5. [SharePoint2010开发入门经典]编译部署SPS WebPart

    本章概要: 1.理解web部件,什么时候需要创建一个 2.理解标准和可视web部件的不同 3.使用VS构建部署web部件

  6. HTML乱码问题

    第一:定义网页显示编码.如果不定义网页编码,那么我们浏览网页的时候,IE会自动识别网页编码,这就有可能会导致中文显示乱码了.所以我们做网页的时候,都会用“<meta http-equiv=”Co ...

  7. POJ 2184

    简单的01背包,把S看体积,把F看价值,把它们变正数处理就可以了.在处理负数时,因为减一个负数相当于加一个,所以要从前往后. #include <iostream> #include &l ...

  8. [HTML 5] Styling with ARIA

    See if you can do a better job styling this button using ARIA states. One huge benefit to styling wi ...

  9. 图像算法研究---Adaboost算法具体解释

    本篇文章先介绍了提升放法和AdaBoost算法.已经了解的可以直接跳过.后面给出了AdaBoost算法的两个样例.附有详细计算过程. 1.提升方法(来源于统计学习方法) 提升方法是一种经常使用的统计学 ...

  10. BAT常问问题总结以及回答(java基础回答一)

    java 基础 八种基本数据类型的大小,以及他们的封装类  答:八种数据类型分别是byte(1字节)-128~127.short(2字节)-32768~32767.char(2字节).int(4字节) ...