POJ 1611 The Suspects 并查集
来源:互联网 发布:东大网络教育 编辑:程序博客网 时间:2024/05/17 08:04
表示刚接触并查集做这道题对并查集有些感觉了,刚开始的时候想的一种方式有bug,最终调试了半个小时终于知道为什么错了 T_T
题目大意:
对于n个学生编号为0至n - 1, 其中编号为0的学生感染了病毒, 接下来给出m组人,每行开头表示该组有多少人,接下来是该组的组员编号,与0在同一组或有同组的人与0在一组的人视为感染,求感染的总人数。
很明显的一个并查集问题,就是分组的问题,问0所在的集合的元素数量。
大致思路:
对于每次给的一个人,都与该组第一个人在同一集合,合并他们所在的集合。
完成所有合并后,扫描一遍0所在集合的元素数量(与0所在集合相同,则所在集合代表元素相同)
代码如下:
Result : Accepted Memory : 388 KB Time : 16 ms
/* * Author : Gatevin * Time : 2014.4.10 17:20 * Problem : POJ 1611 The Suspects * Tag : Disjoint Set */#include<cstdio>int f[30010];int n,m;int find(int i)//压缩路径{ if(i != f[i]) { f[i] = find(f[i]); } return f[i];}void Union(int x, int y)//合并集合{ int rx = find(x); int ry = find(y); if(rx != ry) { f[rx] = ry; } return;}int main(){ while(scanf("%d %d",&n,&m) == 2) { if(!(n || m)) return 0; for(int i = 0; i <= n - 1; i++)//初始化各个集合 f[i] = i; while(m--) { int t,first,tmp; scanf("%d",&t); scanf("%d",&first);//每组的第一个人 for(int i = 1; i <= t - 1; i++) { scanf("%d",&tmp); Union(first,tmp);//连接合并集合 } } int tmp = find(0); int cnt = 1; for(int i = 1; i <= n - 1; i++) { if(find(i) == tmp) cnt++;//统计与0在同一个集合的元素个数 } printf("%d\n",cnt); } return 0;}
0 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(并查集)
- Ruby on Rails: 建立seed database
- 建国初期谁欲将刘少奇拉下马
- 创业公司招聘OpenStack开发人员
- 习近平点赞过约20名干部:均具备实干实绩廉洁
- 介绍一个C# API 生成工具
- POJ 1611 The Suspects 并查集
- 在easyUi datagrid行中增加按扭例子或添加链接
- GDB 基本调试命令
- Linux作业(一)-Linux下文件及目录的权限问题(没试之前我也觉得有点奇怪)
- TinyOs例子之TestSerial学习
- linux下使用多线程的fork和system会出现问题
- 搭建高可用mongodb集群(一)——配置mongodb
- 获取一个类型中某个属性的名称
- jQuery仿百度图片浏览效果