POJ 1611 The Suspects 并查集
来源:互联网 发布:高通unity3d 编辑:程序博客网 时间:2024/06/01 03:57
Problem:
给了一些队伍,求出0号学生所在队伍的人数,每一个学生可以属于多个团队。
Solution:
并查集的模板题,把各个学生merge,然后直接query就可以了。
#include<cstdio>#include<iostream>#include<sstream>#include<cstdlib>#include<cmath>#include<cctype>#include<string>#include<cstring>#include<algorithm>#include<stack>#include<queue>#include<set>#include<map>#include<ctime>#include<vector>#include<fstream>#include<list>using namespace std;#define ms(s) memset(s,0,sizeof(s))typedef unsigned long long ULL;typedef long long LL;const double PI = 3.141592653589;const int INF = 0x3fffffff;int par[30010];void init_par(int n){ for(int i = 0; i <= n; ++i) par[i] = i;}int getPar(int v){ if(par[v] != v) par[v] = getPar(par[v]); return par[v];}int query(int a, int b){ return getPar(a) == getPar(b);}void merges(int a, int b){ par[getPar(a)] = getPar(b);}int main(){// freopen("D:\\input.txt","r",stdin);// freopen("/home/really/Document/output","w",stdout);// ios::sync_with_stdio(false); int m, n, t, f, num, ans; while((~scanf("%d%d", &n, &m)) && n){ ans = 1; init_par(n); for(int i = 0; i < m; ++i){ scanf("%d",&t); if(t != 0) scanf("%d",&f); for(int j = 1 ; j < t; ++j){ scanf("%d", & num); merges(f, num); } } for(int i = 1; i < n; ++i){ if(query(0,i)) ans++; } printf("%d\n",ans); } 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(并查集)
- Android 在win8的cmd中输入adb shell提示不是内部或外部命令
- 编写程序模拟堆栈的存储方式
- Servlet中文乱码
- 4.Median of Two Sorted Arrays
- 从A-za-z0-9任选6个组成一字符串
- POJ 1611 The Suspects 并查集
- 笔记-CCNA与网络安全 第1章 计算机网络
- POJ 2337 Catenyms 欧拉回路
- Numeber类——包装类
- 模块加载过程代码分析1
- 网易秋招编程题——最大的奇约数
- hibernate中的二级缓存的问题
- Serivce通知Activity更新UI方法总结
- js验证url的格式合法性