POJ - 1611The Suspects-并查集
来源:互联网 发布:pdf文件朗读软件 编辑:程序博客网 时间:2024/05/21 19:23
POJ - 1611
The Suspects
Time Limit: 1000MS Memory Limit: 20000KB 64bit IO Format: %I64d & %I64u
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
就是简单检查学生是否与0号患者有间接联系
/*Author: 2486Memory: 584 KBTime: 16 MSLanguage: G++Result: Accepted*/#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int maxn=30000+5;int N,M,par[maxn],ranks[maxn];void init(int sizes) { for(int i=0; i<=sizes; i++) { par[i]=i; ranks[i]=1; }}int find(int x) { return par[x]==x?x:par[x]=find(par[x]);}bool same(int x,int y) { return find(x)==find(y);}void unite(int x,int y) { x=find(x); y=find(y); if(x==y)return ; if(ranks[x]>ranks[y]) { par[y]=x; } else { par[x]=y; if(ranks[x]==ranks[y])ranks[x]++; }}int main() { while(~scanf("%d%d",&N,&M)) { if(N==0&&M==0)break; int k,a,b; init(N); for(int i=0; i<M; i++) { scanf("%d",&k); scanf("%d",&b); a=b; for(int i=1; i<k; i++) { scanf("%d",&a); unite(a,b);//属于同一组则联合 b=a; } } int ans=1; for(int i=1; i<N; i++) { if(find(0)==find(i))ans++;//是否与0号患者有联系 } printf("%d\n",ans); } return 0;}
2 0
- POJ 1611 The Suspects(并查集)
- poj 1611 The Suspects 并查集
- POJ 1611 The Suspects 并查集
- poj 1611 The Suspects(并查集)
- POJ 1611 The Suspects 并查集
- POJ--1611--The Suspects【并查集】
- poj 1611-The Suspects:并查集
- POJ--1611 -- The Suspects [并查集]
- poj 1611 - The Suspects(并查集)
- poj 1611The Suspects 并查集
- poj 1611 The Suspects 并查集
- [并查集]POJ 1611 The Suspects
- poj 1611 The Suspects --- 并查集
- POJ 1611 The Suspects (并查集)
- POJ 1611 The Suspects(并查集)
- POJ 1611--The Suspects 并查集
- POJ 1611 The Suspects 并查集
- poj 1611 The Suspects(并查集)
- 关于Excel文件过大的问题解决方案
- Spring之AOP
- Linux新手要了解的十个知识点
- vi 总结
- WebService流行框架之Axis和CXF
- POJ - 1611The Suspects-并查集
- 黑马程序员--学习OC中类和对象
- java 实现tab页签切换 鼠标悬浮效果
- CAS——性能优化第一步,在线访问外网js
- Android 内存监测工具 DDMS --> Heap
- 利用CXF框架开发WebService(转)
- 湖南省第六届省赛有趣题
- java泛型之基础
- hdu 4336 Card Colletor 动态规划-概率dp