POJ-1611-The Suspects
来源:互联网 发布:淘宝无线店铺新品上架 编辑:程序博客网 时间:2024/05/16 05:08
题目大意是说有n个组,每个组有对应的人(一个人可能在多个组),若有一个人感染SARS,则对应组别的所有人都感染上SARS,题目要求你求出最终能感染此病毒的有多少人
思路:
我的做法是用并查集,把每个组也当作是一个点,则最多有500个点,人的编号就从501开始,在读取数据的时候把每个组内的人组标志设为组名,因为题目中说病毒从1号开始传播,所以最后只需要统计501号人相同的集合有多少人即可。
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn=40510;int ans,n,m,p[maxn];int find(int x){ if(p[x]==-1)return x; return p[x]=find(p[x]);}int main(){ while(scanf("%d%d",&n,&m)&&(n+m)) {ans=0;memset(p,-1,sizeof(p));for(int i=0;i<m;i++){ int k; scanf("%d",&k); for(int j=0;j<k;j++) {int ita;scanf("%d",&ita);ita+=501;int x=find(i);int y=find(ita);if(x!=y) p[x]=y; }}int pos=find(501);for(int i=501;i<=n+501;i++) if(find(i)==pos)ans++;printf("%d\n",ans); } return 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 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
- 在ubuntu上,通过源代码编译安装mysql-5.6.12
- Beijing 2004 / UVa 1203 / UVALive 3135 / ZOJ 2212 Argus (优先队列)
- js 生成缩略图
- Battery driver on Android
- 有用的技术链接
- POJ-1611-The Suspects
- S3C2440 UART中断方式
- 用 Python 替代 Bash 脚本
- Ubuntu 12.04中文输入法的安装
- 一个有意思的小程序,考验多线程问题能力
- 收集一些jQueryMobile的插件和案例
- maven添加jar文件到本地库
- h.264 SPS PPS解析源代码,C实现
- PMIC driver on Android