poj 1611 The Suspects(简单并查集)
来源:互联网 发布:小成图排盘软件 编辑:程序博客网 时间:2024/05/22 11:50
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10536
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>using namespace std;const int MAXN=3e4+5;struct node{ int f[MAXN]; void init(int n) { for(int i=0;i<n;i++) f[i]=i; } int ff(int x) { if(f[x]!=x) f[x]=ff(f[x]); return f[x]; } void join(int a,int b) { f[ff(a)]=f[ff(b)]; } bool find(int a,int b) { return ff(a)==ff(b); } int count(int n) { int t[MAXN]; for(int i=0;i<n;i++) t[i]=ff(i); sort(t+1,t+n+1); return unique(t+1,t+n+1)-(t+1); }}soul;int b[MAXN]; //找到0的团体的成员总数即可int main(){ //freopen("cin.txt","r",stdin); int n,m; while(cin>>n>>m){ if(n==0&&m==0)break; soul.init(n); for(int i=0;i<m;i++){ int k; scanf("%d",&k); if(k>0)scanf("%d",&b[0]); for(int j=1;j<k;j++){ scanf("%d",&b[j]); soul.join(b[j-1],b[j]); } } int ans=0; for(int i=0;i<n;i++){ if(soul.find(0,i))ans++; } printf("%d\n",ans); } return 0;}
0 0
- zoj 1789||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 并查集简单题
- 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:并查集
- HTML5 常用语法一览(列举不支持的属性)
- 分布式事务-阿里
- 从头新建VPC
- Android初学习 - 在布局中动态添加view的两种方法
- 百练 2813:画家问题
- poj 1611 The Suspects(简单并查集)
- Oracle trunc 用法详解
- php快速入门基本概念
- 如何实现让WIN7、Winserver2008开机不输入密码直接进入系统
- IP协议解读(一)
- struts2框架原理
- Vijos1932重要的誓言题解
- 【unity填坑日记】Array[] Contains导致IOS AOT编译错误
- 百练 2723:不吉利日期