POJ 1611 The Suspects【并查集】
解题思路:一共给出 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
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
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
#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【并查集】的更多相关文章
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
随机推荐
- swift的属性与变量- Stored Properties and Instance Variables
是一个概念 Stored Properties and Instance Variables If you have experience with Objective-C, you may know ...
- javaScript 计算两个日期的天数相差
一:计算两个日期相差的天数 1 <html> <head> <meta http-equiv="Content-Type" content=" ...
- 服务器安装ESXI6.5系统
服务器做raid5磁盘冗余配置步骤 1.打开服务器电源,进入服务器启动界面,按下F10后回车,等待服务器回应你的操作进入下一个页面 提示: 在进入服务启动页面,在页面的最下方就可以看到F9,F10, ...
- iOS 小知识
iOS 各版本: http://www.pig66.com/2018/145_1021/17357553.html
- Fiddler4抓包工具使用教程
本文参考自http://blog.csdn.net/ohmygirl/article/details/17846199,纯属读书笔记,加深记忆 1.抓包工具有很多,为什么要使用Fiddler呢?原因如 ...
- IOS与h5交互记录
博主之前做过移动端app嵌入网页,与Android和IOS有交互,一直没有时间分享过程.这里不多说Android交互啦-很简单,详细了解IOS与h5的交互吧. IOS不同语法和h5的交互所建立的JSB ...
- P1421 小玉买文具
... 题目描述 班主任给小玉一个任务,到文具店里买尽量多的签字笔.已知一只签字笔的价格是1元9角,而班主任给小玉的钱是a元b角,小玉想知道,她最多能买多少只签字笔呢. 输入输出格式 输入格式: 输入 ...
- Windows环境下制作Ubuntu的U盘启动工具
Windows用户想要尝试一下Ubuntu或其他Linux系统?最简单的方法就是使用VMware Workstation,只是虚拟机会真实占用内存,如果你的电脑内存8G及以上可以试试.内存偏小,或者觉 ...
- Problem 8
Problem 8 # Problem_8.py """ The four adjacent digits in the 1000-digit number that h ...
- 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然在没有一直往右走然后走到头再往上走一格再往左走到头之前. 肯定是一直在蛇形走位.. 这个蛇形走位的答案贡献可以预处理出来.很容易 ...