POJ 1611 The Suspects (并查集求数量)
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
大意:
有n个人编号为0~n,m个团队,每个团队开头k表示k个人,然后是k个人的编号,编号为0的是嫌疑人,和嫌疑人在一个团队的也是嫌疑人,求嫌疑人的数量。
解题思路:
先输入n个学生,对fa[]初始化,然后输入m个团队的第一个人fir,后每输入一个人与fir比较,并放在一个集合,最后判断0~n个人有多少在0集合里。
并查集链接
#include<cstdio>
int n,m,ans,i,j,k,fir,fa[+],a;
int find(int a)
{
if(a == fa[a])
{
return a;
}
else
{
return fa[a]=find(fa[a]);
}
}
void f1(int x,int y)
{
int nx,ny;
nx=find(x);
ny=find(y);
if(ny == )
{
fa[nx]=ny;
}
else
{
fa[ny]=nx;
}
}
int main()
{
while(scanf("%d %d",&n,&m) && (m+n))
{
for(i = ; i < n ; i++)
{
fa[i]=i;
}
for(i = ; i < m ; i++)
{
scanf("%d %d",&k,&fir);
for(j = ; j < k ; j++)
{
scanf("%d",&a);
f1(fir,a);
}
}
ans=;
for(i = ; i < n ; i++)
{
if(find(i) == )
{
ans++;
}
}
printf("%d\n",ans);
}
}
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 (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- The Suspects(并查集求节点数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28164 Accepted: 13718 De ...
- 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 ...
随机推荐
- PostgreSQL - 转义字符
转载至:postgresql字符转义 前言 在PostgreSQL 9之前的版本中,可以直接使用反斜杠\进行转义:比如:\b表示退格, \n表示换行, \t表示水平制表符,\r标示回车,\f表示换页. ...
- 跟我一起玩Win32开发(6):创建右键菜单
快捷菜单,说得容易理解一点,就是右键菜单,当我们在某个区域内单击鼠标右键,会弹出一些菜单项.这种类型的菜单,是随处可见的,我们在桌面上右击一下,也会弹出一个菜单. 右键菜单的好处就是方便,它经常和我们 ...
- Android课程设计第一天Android Studio安装
注意:课程设计只为完成任务,不做细节描述~ 学校有一个Android的课设,所以顺便把Android Studio安装了上去. 实际上安装过程并不复杂,只有几个地方需要注意~ 安装包可以去http:/ ...
- Lucky Array Codeforces - 121E && Bear and Bad Powers of 42 Codeforces - 679E
http://codeforces.com/contest/121/problem/E 话说这题貌似暴力可A啊... 正解是想出来了,结果重构代码,调了不知道多久才A 错误记录: 1.线段树搞混num ...
- 中国剩余定理 POJ 1006 Biorhythms
题目传送门 题意:POJ有中文题面 分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板 代码: /********* ...
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
- Plugging an Unplugged Pluggable Database
1.unplug To unplug a PDB, you first close it and then generate an XML manifest file. The XML file co ...
- HTML form label
在表单布局中会遇到label标签的使用,label没有任何样式效果,有触发对应表单控件功能.比如我们点击单选按钮或多选框前文字对应选项就能被选中,这个就是对文字加了<label>标签实现. ...
- [在读]HTML5程序设计(第二版)
去年买的,看了30%不到,之后一直是搁置状态,内容还不错,确确实实纯粹讲H5的.
- java设计模式之单例设计模式
单例设计模式 保证一个类在使用过程中,只有一个实例.优势就是他的作用,这个类永远只有一个实例. 优势:这个类永远只有一个实例,占用内存少,有利于Java垃圾回收. 单例设计模式关键点 私有的构造方法. ...