POJ 1611 The Suspects (并查集+数组记录子孙个数 )
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 24134 | Accepted: 11787 |
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
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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#define N 30000 using namespace std;
int n, m;
int cnt[N];
int fa[N]; //0 < n <= 30000 and 0 <= m <=500
void init()
{
for(int i=; i<N; i++)
{
fa[i]=i;
cnt[i]=;
}
} int findset(int x)
{
return fa[x]!=x? fa[x]=findset(fa[x]):x;
} void union_set(int x, int y)
{
int xx=findset(x);
int yy=findset(y);
if(xx==yy) return; //说明两元素本来就属于同一个集合 返回
else if(xx<yy) //如果x的根节点比y的根节点 小
{
fa[yy]=xx;
cnt[xx]=cnt[xx]+cnt[yy];
}
else if(xx>yy)
{
fa[xx]=yy;
cnt[yy]=cnt[yy]+cnt[xx];
}
} int main()
{
int dd, a, b;
while(scanf("%d %d", &n, &m)!=EOF)
{
if(n== && m==) break;
init();
for(int i=; i<m; i++)
{
scanf("%d", &dd);
scanf("%d", &a);
for(int j=; j<dd-; j++)
{
scanf("%d", &b);
union_set(a, b);
a=b;
}
}
printf("%d\n", cnt[] );
}
return ;
}
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 (并查集求数量)
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-1703 Find them, Catch them(并查集&数组记录状态)
题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
随机推荐
- (2)Unity3d菜单
1.文件菜单 2.编辑菜单 Frame Selected 焦点选择和双击Hierarchy中的元素一样的功能 Lock View to Selected 选中对象再点击此按钮,在场景视图中移动此对象 ...
- 初级Springboot(一)
初级Springboot(一) 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 一.了解Springboot 做Java开发的小伙伴都知道,我们在做项目的时候,需要去写大量的配置文件 ...
- Careercup | Chapter 3
3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...
- cef 下载地址
最新的CEF3源代码在:http://cefbuilds.com/CEF3的论坛:http://www.magpcss.org/ceforum/viewforum.php?f=5CEF3 C++开发环 ...
- Typora +google + Markdown Here 公众号
一劳永逸的公众号排版方法 http://mp.weixin.qq.com/s/zb-YaacNLggG2-njF5HJ0A
- mac下报错 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
如题mac下遇到错误: 解决办法:安装mac的命令行工具CommandLineTools xcode-select --install
- solr 最佳实践
管理页面 页面地址:http://{ip}:{port}/solr/#/ 管理页面的data-import页可以手动重建索引,configuration指定了数据源,重建索引也可以通过http请求触发 ...
- SQL_字符操作函数
原创作品.出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明下面出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...
- cocos2dx3.0 对象池
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdzE4NzY3MTA0MTgz/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- idea常用的快捷命令
main方法: psvm System.out.println(): sout