The Suspects(并查集求节点数)
| Time Limit: 1000MS | Memory Limit: 20000K | |
| Total Submissions: 28164 | Accepted: 13718 |
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>
#include<string.h>
const int MAXN=;
int pre[MAXN],num[MAXN];
int find(int x){
return pre[x]=x==pre[x]?x:find(pre[x]);
}
int merge(int x,int y){
int f1,f2;
f1=find(x);f2=find(y);
if(f1!=f2){
if(f1<f2)pre[f2]=f1,num[f1]+=num[f2];
else pre[f1]=f2,num[f2]+=num[f1];
}
}
int main(){
int n,m,k,a,b;
while(scanf("%d%d",&n,&m),n|m){
for(int i=;i<n;i++){
pre[i]=i;
num[i]=;
}
while(m--){
scanf("%d",&k);
scanf("%d",&a);
for(int i=;i<k;i++){
scanf("%d",&b);
merge(a,b);
}
}
printf("%d\n",num[]);
}
return ;
}
The Suspects(并查集求节点数)的更多相关文章
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
- hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 2545(并查集求节点到根节点的距离)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2545 思路:dist[u]表示节点u到根节点的距离,然后在查找的时候更新即可,最后判断dist[u], ...
- 杭电 2120 Ice_cream's world I (并查集求环数)
Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_ ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- The Suspects(并查集维护根节点信息)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 37090 Accepted: 17980 De ...
随机推荐
- leetcode算法刷题(二)——动态规划(一)
上次刷了五六道题,都是关于string处理的,这次想换个知识点刷一下,然后再回头刷string的题,当做复习.. 这几天主要会选择动态规划的题目,因为以前从没刷过这方面的东西,很多东西都不是很懂..就 ...
- 可以使用QT给龙芯开发软件
直接apt-get install libqt5core5a就有了,也许是一个很好的小众市场机会呢 至于系统,可以使用debian mips https://www.debian.org/devel/ ...
- 一步一步学习SignalR进行实时通信_6_案例
原文:一步一步学习SignalR进行实时通信_6_案例 一步一步学习SignalR进行实时通信\_6_案例1 一步一步学习SignalR进行实时通信_6_案例1 前言 类的定义 各块功能 后台 上线 ...
- aix5.1 5.2 5.3 6.1 7.1运维技术总结
++++++++++++++++++++++++++++ + Ruiy 20014-10 zz + +Technology Area; + Tel:150 55198367 + QQ:5160 591 ...
- linux备份mysql数据库
刚学的技术,保存一下 1. 在linux服务器上创建一个 msyql.sh文件 (随便定义) ,用来保存备份mysql数据的脚本 2. 编辑mysql.sh文件 (自己之前写过的一个脚本) mys ...
- Android UI ActionBar功能-自定义Tab功能
还可以使用ActionBar实现Tab选项卡功能: 官方帮助文档:http://wear.techbrood.com/training/basics/actionbar/styling.html#Cu ...
- iOS_根据文字字数动态确定Label宽高
iOS7中用以下方法 CGSize 替代过时的iOS6中的- (CGSize)sizeWithFont:(UIFont *)font 方法 // iOS7_API_根据文字 字数动态确定Label宽高 ...
- JSP 文件上传下载系列之二[Commons fileUpload]
前言 关于JSP 文件上传的基础和原理在系列一中有介绍到. 这里介绍一个很流行的组件commons fileupload,用来加速文件上传的开发. 官方的介绍是: 让添加强壮,高性能的文件到你的se ...
- Afinal开源框架中FinalActivity的使用
1. 首先将afinal.jar文件复制到项目中的libs文件夹下 2. 让MainActivity不在继承系统的Activity,而是继承FinalActivity public class Mai ...
- Java面试题之Class.forName的作用
按参数中指定的字符串形式的类名去搜索并加载相应的类,如果该类字节码已经被加载过,则返回代表该字节码的Class实例对象,否则,按类加载器的委托机制去搜索和加载该类,如果所有的类加载器都无法加载到该类, ...