The Suspects(并查集)
来源:互联网 发布:app数据接口 编辑:程序博客网 时间:2024/05/22 14:48
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
题目大意:第一个数是人物角标上限,第二个数n是告诉你有几组人,接下来的n行的开头第一个数告诉你每一组有多少人。刚开始只有0一个人是可能患者,但是因为存在各组之间的人员交流,所以会传染,一旦一组里面有一个人是可能患者,那么这个组里面的所以人就都是可能患者,输出是可能患者的总人数。
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10536
源代码:
#include<cstdio>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<string>using namespace std;int par[30005];int rank[30005];int n,t;void init(){ for(int i=0;i<30005;i++) { par[i]=i; rank[i]=1; }}int find(int x){ if(x==par[x]) return x; return par[x]=find(par[x]);}void unite(int y,int d){ int yy=find(y); int dd=find(d); if(yy!=dd) { par[yy]=dd; rank[dd]=rank[dd]+rank[yy]; }}int main(){ while(~scanf("%d%d",&n,&t)) { if(n+t==0) break; init(); while(t--) { int x,y,d; scanf("%d",&x); int i; scanf("%d",&y); for(i=0;i<x-1;i++) { scanf("%d",&d); unite(y,d); } } int ans=find(0); printf("%d\n",rank[ans]); } return 0;}
0 0
- poj1611 The Suspects(并查集)
- POJ1611 The Suspects (并查集)
- The Suspects 1611 (并查集)
- The Suspects(并查集问题)
- The Suspects(并查集)
- poj The Suspects(并查集)
- POj1611:The Suspects(并查集)
- POJ_1611-The Suspects(并查集)
- The Suspects (并查集)
- 【POJ1611】The Suspects(并查集)
- The Suspects (并查集)
- The Suspects(并查集)
- poj1611 the suspects(并查集)
- POJ1611 The Suspects(并查集)
- The Suspects 并查集
- The Suspects(并查集)
- 并查集 The Suspects
- The Suspects 并查集
- Android-Dialog
- VLC-Android Intro - Modules Init
- type= image 表单重复提交问题
- UVALive 4670(AC自动机)
- 深入浅出node.js游戏服务器开发1——基础架构与框架介绍
- The Suspects(并查集)
- jar 打包命令详解
- 静态泛型
- MySQL联合查询
- TCP状态转换详解
- 总结一下最近的体会
- openwrt tftp 烧录openwrt-ramips-mt7628-mt7628-squashfs-sysupgrade.bin
- crc 校验
- 泛型的使用方法以及注意事项