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 ...
随机推荐
- Vim 插件管理及安装
1.先将ubuntu1204的软件源进行更新.sudo apt-get update 2.再在终端中敲如下命令,让程序自动安装,根据网速的好坏安装时间有长有短. wget -qO- https://r ...
- 滚动效果--marquee的使用
1. <marquee></marquee>标签,默认从最右侧往左滚动: 2. marquee 支持的属性 (1)behavior设置滚动方式: <marquee beh ...
- linux部署.net core api并且实现上传图片
为了体验.net在linux上运行,所以使用HttpClient东借西抄做了一个简单的api上传功能. 第一步,简单的上传功能: public class UploadHelper { private ...
- [luogu4310] 绝世好题 (递推)
传送门 题目描述 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). 输入输出格式 输入格式: 输入文件共2行. 第一行包括 ...
- keepalived安装实现nginx主备高可用
. keepalived安装环境 su - root yum -y install kernel-devel* yum -y install openssl-* yum -y install popt ...
- jQuery的父,子,兄弟节点查找方法
以下罗列一下jQery下节点查找的方法: jQuery.parent(expr) 找父亲节点.能够传入expr进行过滤,比方$("span").parent()或者$(" ...
- XTU1201:模和除
题目描写叙述 两个整数x和y,满足1<=x<=a,1<=y<=b 且x%y等于x/y的x和y的对数有多少? x%y是x除以y的余数, x/y是x除以y的商,即整数除. 输入 不 ...
- cocos2d-x 3.2 之 2048 —— 第五篇
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Manarcher 求 字符串 的最长回文子串 【记录】
声明:这里仅仅写出了实现过程.想学习Manacher的能够看下这里给出的实现过程,算法涉及的一些原理推荐个博客. 给个链接 感觉讲的非常细 引子:给定一个字符串s,让你求出最长的回文子串的长度. 算法 ...
- c5
// // main.c // Switch练习2 // // Created by xiaomage on 15/6/6. // Copyright (c) 2015年 xiaomage. All ...