The Suspects

Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 55832   Accepted: 26501

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

Source

本题思路:对于加入的两个点进行合并操作,保证每个边被合并了一次即可。查找时寻找头结点与0的头结点相等的元素的个数。
 #include <cstdio>
using namespace std; const int maxn = + ;
int n, m, num, ans, head[maxn], value[maxn]; int find(int x) {
if(x == head[x]) return x;
return head[x] = find(head[x]);
} void Union(int value[maxn], int num) {
for(int i = ; i < num; i ++) {
int fx = find(value[]);
int fy = find(value[i]);
if(fx == fy) continue;
head[fx] = fy;
}
} int main () {
int x;
while(scanf("%d %d", &n, &m) == && (n || m)) {
ans = ;
for(int i = ; i < n; i ++) head[i] = i;
for(int i = ; i < m; i ++) {
scanf("%d", &num);
for(int j = ; j < num; j ++)
scanf("%d", &value[j]);
Union(value, num);
}
for(int i = ; i < n; i ++) find(i);
int i = head[];
for(int j = ; j < n; j ++)
if(head[j] == i) ans ++;
printf("%d\n", ans);
}
return ;
}

POJ-1611.TheSuspects.(并查集)的更多相关文章

  1. C - The Suspects POJ - 1611(并查集)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  2. poj 1611 简单并查集的应用

    #include<stdio.h> #define N 31000 int pre[N]; int find(int x) { if(x!=pre[x])     pre[x]=find( ...

  3. poj 2524 (并查集)

    http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...

  4. [POJ 2588]--Snakes(并查集)

    题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS   Memory Limit: 65536K   Description B ...

  5. poj 1456 Supermarket - 并查集 - 贪心

    题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...

  6. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  7. poj 1182 (关系并查集) 食物链

    题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...

  8. Poj(1182),种类并查集

    题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...

  9. Poj(1703),种类并查集

    题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...

  10. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

随机推荐

  1. Ubuntu 安装 ansible

    sudo apt update sudo apt-get install software-properties-common sudo apt-add-repository --yes ppa:an ...

  2. wordpress系统网站访问慢的解决方案

    从2013年5月底开始,google在中国基本处于无法访问状态,谷歌官网 域名,香港域名均无法访问,就连之前的IP访问方法也都失效,而Google Adsense打不开,恐怕做谷歌联盟的站长也要倒霉了 ...

  3. MT41J256M16HA-125 原厂订购 现货销售

    作为一家科研公司,保证芯片的原厂品质和正规采购渠道是科学严谨的研发工作中重要的一环,更是保证研发产品可靠.稳定的基础.而研发中所遇到的各种不可预测的情况更是每个工程师向技术的山峰攀登中时会遇到的各种难 ...

  4. leetcode69. x 的平方根 🌟

    题目: 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 ...

  5. Codeforces Round #425 (Div. 2) - A

    题目链接:http://codeforces.com/contest/832/problem/A 题意:有n个棍子,两个人轮流取这些棍子,每个人每次只能去恰好k个棍子(不足k个则不能取),问先手取的棍 ...

  6. bzoj4543 [POI2014]Hotel加强版 长链剖分+树形DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4543 题解 这道题的弱化版 bzoj3522 [POI2014]Hotel 的做法有好几种吧. ...

  7. flask之Twitter Bootstrap

    一:Twitter Bootstrap是什么? 1.开源框架:提供用户页面组件. 2.可以创建整洁且具有吸引力的网站,并且网站能兼容所有现代的Web浏览器. 特点: Bootstrap 是客户端框架, ...

  8. springboot--异步执行的方法及定时执行的方法

    让方法被调用后异步的执行 一般来说,要异步执行一个任务都是创建一个线程来专门干这个任务.在springboot中有 @Async 这个注解快速实现方法的异步执行.只需要两步:第一步: 在启动类上加上@ ...

  9. 22.Express框架——2019年12月19日

    2019年12月19日14:16:36 1. express简介 1.1 介绍 Express框架是后台的Node框架,所以和jQuery.zepto.yui.bootstrap都不一个东西. Exp ...

  10. 【leetcode】1046. Last Stone Weight

    题目如下: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose t ...