The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 28164   Accepted: 13718

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>
#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(并查集求节点数)的更多相关文章

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

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

  2. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

  3. hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. hdu 2545(并查集求节点到根节点的距离)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2545 思路:dist[u]表示节点u到根节点的距离,然后在查找的时候更新即可,最后判断dist[u], ...

  5. 杭电 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_ ...

  6. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  7. 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 ...

  8. 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 ...

  9. The Suspects(并查集维护根节点信息)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 37090   Accepted: 17980 De ...

随机推荐

  1. android xml解析 sax

    1.简要概述 Android 各种文件都是xml格式的,还有标准的webservice返回的是xml文件,虽然现在的json使用在移动设备端越来越广泛,但是xml格式的解析感觉还是相当必要. 2.sa ...

  2. Example: Develop Web application on Baidu App Engine using CherryPy

    In the past few months, I have developed two simple applications on Baidu App Engine. Compared to Go ...

  3. service:jmx:rmi:///jndi/rmi

    service:jmx:rmi:///jndi/rmi://ip:9889/jmxrmi http://stackoverflow.com/questions/2768087/explain-jmx- ...

  4. javascript预加载和延迟加载

    延迟加载javascript,也就是页面加载完成之后再加载javascript,也叫on demand(按需)加载,一般有一下几个方法: What can your tired old page, o ...

  5. poj1007 qsort快排

    这道题比较简单,但通过这个题我学会了使用c++内置的qsort函数用法,收获还是很大的! 首先简要介绍一下qsort函数. 1.它是快速排序,所以就是不稳定的.(不稳定意思就是张三.李四成绩都是90, ...

  6. 最短路(dijskra+SPFA+Bellman)

    最短路 Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  7. 赵雅智_BroadcastReceiver电话监听

    AndroidManifest.xml 注冊广播接收者 加入权限 <?xml version="1.0" encoding="utf-8"?> &l ...

  8. 1005 Number Sequence(HDU)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...

  9. android入门——BroadCast(2)

    自定义广播 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=&q ...

  10. sql server 存储过程、事务,增删改

    CREATE procedure [dbo].[sp_TableSave] @TypeID tinyint, -- 0 新增,1 修改,2 删除 @ID int, ), ), ), @UID int, ...