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. React 同构

    React 同构 搬运 https://segmentfault.com/a/1190000004671209 究竟什么是同构呢? 同构就是希望前端 后端都使用同一套逻辑 同一套代码 Nodejs出现 ...

  2. 我的android studio

  3. coroutine

    在脚本语言中,coroutine 不是个新鲜词汇,比如 lua 内建 coroutine,python中的greenlet,但在C程序中,并不是太常见. windows 下有 fiber,相关函数为 ...

  4. 普林斯顿大学算法课 Algorithm Part I Week 3 排序算法复杂度 Sorting Complexity

    计算复杂度(Computational complexity):用于研究解决特定问题X的算法效率的框架 计算模型(Model of computation):可允许的操作(Allowable oper ...

  5. LightOJ 1085(树状数组+离散化+DP,线段树)

    All Possible Increasing Subsequences Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format: ...

  6. 飘逸的python - 两种with语句实现方法

    第一种是实现上下文管理器协议,即魔法方法__enter__和__exit__. class Foo: def __enter__(self): print 'in' def __exit__(self ...

  7. T-SQL变量

    T-SQL中变量分为全局变量和局部变量,分别使用@@和@前缀. 全局变量 常用的全局变量有@@VERSION .@@IDENTITY.@@ERROR.@@ROWCOUNT 用法 select @@VE ...

  8. javascript高级知识分析——实例化

    代码信息来自于http://ejohn.org/apps/learn/. new做了什么? function Ninja(){ this.name = "Ninja"; } var ...

  9. Eclipse配置Hadoop开发环境

    Step 1:选择Hadoop版本对应的Eclipse插件jar包(可自行编译),我的Hadoop版本是hadoop-0.20.2,对应的插件应该是:hadoop-0.20.2-eclipse-plu ...

  10. VMware vSphere Client为虚拟机制定物理网卡(图文并茂)

    1.首先,查看我的服务器有几张网卡,如下图共3张,接下来我将为虚拟主机制定一张网卡,以及为当中的两台虚拟的CentOS7各制定一张网卡. 2.打开“硬件”---->“网络”,如图,已经启用一张网 ...