The Suspects 并查集
来源:互联网 发布:动态代理 java 编辑:程序博客网 时间:2024/05/03 01:00
传染病 一开始只是0可能有 然后他的圈子里的人可能都被感染,感染的人可能又在别的圈子里所以是典型的并查集 在一个圈子里的都放到一个集合里 然后最后找到包含0的圈子就是所有的感染者
Description
严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁。为了减少传播给别人的机会,最好的策略是隔离可能的患者。
在Not-Spreading-Your-Sickness大学( NSYSU), 有许多学生团体。同一组的学生经常彼此相通,一个学生可以同时加入几个小组。为了防止非典的传播,NSYSU收集了所有学生团体的成员名单。他们的标准操作程序(SOP)如下:
一旦一组中有一个可能的患者, 组内的所有成员就都是可能的患者。
然而,他们发现当一个学生被确认为可能的患者后不容易识别所有可能的患者。你的工作是编写一个程序, 发现所有可能的患者。
Input
输入文件包含多组数据。
对于每组测试数据:
第一行为两个整数n和m, 其中n是学生的数量, m是团体的数量。0 < n <=30000,0 <= m <= 500。
每个学生编号是一个0到n-1之间的整数,一开始只有0号学生被视为可能的患者。
紧随其后的是团体的成员列表,每组一行。
每一行有一个整数k,代表成员数量。之后,有k个整数代表这个群体的学生。一行中的所有整数由至少一个空格隔开。
n = m = 0表示输入结束,不需要处理。
Output
对于每组测试数据, 输出一行可能的患者。
Sample Input
100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0
Sample Output
411
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 30020
using namespace std;
int par[N];
int number[N];
int fond(int x)//查找根
{
return x == par[x] ? x :fond(par[x]);
}
void unionn(int x, int y)//合并两个数
{
int root1,root2;
root1 = fond(x);
root2 = fond(y);
if(root1 != root2)
{
par[root2] = root1;//合并
number[root1]+=number[root2];//一个圈子的人数统计出来
}
}
int main()
{
int n,m,num;
while(scanf("%d%d",&n,&m) == 2 && (m+n))
{
for(int i = 0; i < n; i++)
{
par[i] = i;
number[i] = 1;
}
for(int i = 0; i < m; i++)
{
scanf("%d",&num);
int stu[num];
memset(stu,0,sizeof(stu));
for(int j = 0; j < num; j++)
{
scanf("%d",&stu[j]);
if(j >= 1)
unionn(stu[j-1],stu[j]);
}
}
printf("%d\n",number[fond(0)]);
}
return 0;
}
- The Suspects 并查集
- The Suspects(并查集)
- 并查集 The Suspects
- The Suspects 并查集
- 并查集 - The Suspects
- The Suspects(并查集)
- 并查集----The Suspects
- The Suspects 并查集
- The Suspects -- 并查集
- 1611 The Suspects 并查集
- POJ 1611 The Suspects(并查集)
- POJ1611 The Suspects 并查集求解
- poj 1611 The Suspects 并查集
- POJ 1611 The Suspects 并查集
- POJ1611 The Suspects 并查集
- zoj 1789The Suspects 并查集
- poj1611 The Suspects (并查集)
- The Suspects 并查集基础
- SOCKET网络通信
- 随手写了一个鸡肋的二叉树
- phg不能识别Trilinos
- _25407739的博客 个人主页|我的博客
- 计算两点间的距离
- The Suspects 并查集
- MyEclipse使用总结——MyEclipse10安装SVN插件
- java多线程ThreadLocal源码小记
- mysql的group by应用
- EEPROM、EPROM、PROM、RAM、DRAM与Flash区别
- hdu2482 transit search
- 执行start-dfs.sh后,datenode没有启动
- 输入n ,打印出n*n的螺旋矩阵(算法)
- Maven(项目管理工具)