UVA 1197 The Suspects
来源:互联网 发布:手机版特效软件 编辑:程序博客网 时间:2024/06/05 05:43
题意:有0到n-1个学生,0号学生的感染者,现在给出m个小组,每个小组的人相互接触过,如果a和b接触,b和c接触,则a和c也算接触过。问感染者有多少个。
做法:并查集
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int fa[33333];void init(int n){ for(int i = 0 ; i < n ; i++) fa[i] = i;}int getfa(int x){ return fa[x] == x ? x : fa[x] = getfa(fa[x]);}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { if(m == 0 && n == 0) return 0; init(n); for(int i = 0 ; i < m ; i++) { int t,a[33333]; scanf("%d",&t); for(int j = 1 ; j <= t ;j++) scanf("%d",&a[j]); for(int j = 1 ; j <= t-1 ; j++) { if(getfa(a[j])!=getfa(a[j+1])); fa[getfa(a[j+1])] = getfa(a[j]); } } int ans = 0; for(int i = 0 ; i < n ; i++) { if(getfa(0) == getfa(i)) ans ++; } printf("%d\n",ans); } return 0;}
0 0
- UVA 1197 - The Suspects
- UVA 1197 The Suspects
- UVa 1197 - The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- 【转】在TCL中安装非市场应用
- Mac下使用SAE的问题们
- my—review-2014
- 【Uva 340】 Master-Mind Hints
- HDU 1241 Oil Deposits
- UVA 1197 The Suspects
- 技术管理的核心内容: 提高团队技能
- 打印队列(Printer Queue)
- 欢迎使用CSDN-markdown编辑器
- Clone Graph
- HDU 1829 A Bug's Life
- 【android】类似微信底部按钮标签实现
- HDU 1253 胜利大逃亡
- iOS开发——圆形过渡动画