POJ 1611 The Suspects(并查集)
来源:互联网 发布:mac的dare you试色 编辑:程序博客网 时间:2024/06/03 06:02
思路:直接用并查集,最后找到 0 所在的集合,把 集合中的 人数 输出即可
#include<iostream>using namespace std;const int maxn=30000 +100;int set[maxn];int sum[maxn];int set_find(int d){if(set[d]<0)return d;return set[d]=set_find(set[d]);}void join(int x,int y){x=set_find(x);y=set_find(y);set[x]=y;}int main(){int n,m;int k;while(cin>>n>>m){if(n==0 &&m==0)break;memset(set,-1,sizeof(set));memset(sum,0,sizeof(sum));int i;for(i=0;i<n;i++)sum[i]=1;while(m--){cin>>k;int a,b;k--;cin>>a;while(k--){cin>>b;if(set_find(a)!=set_find(b)){sum[set_find(b)]=sum[set_find(b)]+sum[set_find(a)];//先求和join(set_find(a),set_find(b));//再 合并,把 根合并 }a=b;}}cout<<sum[set_find(0)]<<endl;}}
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(并查集)
- 面试常见问题整理----oracle
- Window Stations 和桌面
- Xcode无法选择模拟器运行
- Objective-C基础之权限修饰符private, protected和public
- ★ HDU 3667 费用与流量平方成正比的最小流
- POJ 1611 The Suspects(并查集)
- 二分图最大匹配总结
- android中支持多种文件类型的下载类
- listView分页加载数据
- Codeforces 52C Circular RMQ 线段树+从字符串读数字 sscanf
- 4 Maya模型基础--创建多边形工具、连接表面、补洞
- window2003 应用程序正常初始化失败(0xc0000135)
- 贝叶斯分类算法
- Qt浅谈之十四:文件操作