POJ 1611 The Suspects (并查集)
来源:互联网 发布:鸟巢大棚建造数据 编辑:程序博客网 时间:2024/05/01 05:44
题目链接: http://vjudge.net/problem/viewProblem.action?id=10536
题目意思:给你 n 个人 m 个小组 , 给m行 每行描述属于这个组的人的编号。每个人的编号从 0 到 N-1 ; 0这个学生是被感染的,跟他同个组织的人都被感染,而且只要某个组里有一个人感染则其他人也感染。让你判断有多少人感染。
分析: 并查集的应用,判断连通性。
#include<stdio.h>#include<cmath>#include<iostream>#include<algorithm>#include<string.h>#include<queue>using namespace std;#define MAX 30005int father[MAX];int find(int x) //朴素的查找方法{ while(father[x] != x) { x = father[x]; } return x;}void Merge(int a, int b){ int aa = find(a); int bb = find(b); if(aa == bb) return ; else father[aa] = bb;}int a[MAX];int main(){ int i,j,k,n,m; while(scanf("%d%d",&n,&m) != EOF ) { if(n==0&& m==0 ) break; for(i=0;i<n;i++) father[i] = i; int ans = 0; while(m--) { scanf("%d%d",&k,&a[0]); for(i=1;i<k;i++) { scanf("%d",&a[i]); Merge(a[0],a[i]); } } int root = find(0); //一开始这里写成if (fine(0) == find (i)) for(i=0;i<n;i++) // 一直TLE,主要是因为前面的find没有用到路径压缩所以超时。 { if(root == find(i)) ans++; } printf("%d\n", ans); } return 0;}
int find (int x ) //路径压缩 节省很多时间{ if(x != father[x]) return find(father[x]); return x ;}
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(并查集)
- ArcGIS之计算面要素的宽度
- 关于Java 字符流和字节流
- Java设计模式--适配器(Adapter)模式
- zookeeper client分析 ***********************8
- 镇江锅盖面
- POJ 1611 The Suspects (并查集)
- UVA10534 - Wavio Sequence - 动态规划
- 大话设计模式2 策略模式 的java代码实现
- C++11线程指南(7)--资源竞争条件
- C 语言 洗牌程序
- Codeforces #260 (Div. 2) B. Fedya and Maths(数学:水题)
- zookeeper watcher功能分析
- pc端、移动端字体的设置
- 知其然(1) 无法使用 DISTINCT, GROUP BY 等子句从视图中选择 ROWID 或采样