poj 1611 The Suspects
来源:互联网 发布:linux dns文件位置 编辑:程序博客网 时间:2024/06/08 08:59
点击打开链接poj 1611
思路:最简单的并查集应用
分析:在输入的时候把在同一个集合里面的元素全部合并起来,然后最后在找有几个元素和0的根节点相同即可。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 30010;int n , m;int num[MAXN];int father[MAXN];void init(){ for(int i = 0 ; i <= n ; i++){ father[i] = i; num[i] = 1; }}int find(int x){ if(x != father[x]) father[x] = find(father[x]); return father[x];}int main(){ int k , x; while(scanf("%d%d" , &n , &m) && n+m){ init(); while(m--){ scanf("%d" , &k); int root; for(int i = 0 ; i < k ; i++){ scanf("%d" , &x); if(i == 0) root = find(x); else{ int fx = find(x); if(fx != root){ father[fx] = root; num[root] += num[fx]; } } } } printf("%d\n" , num[find(0)]); } return 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 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
- RTX反向登录
- 数据库建表原则【鸡蛋】
- 关于pragma pack的用法(一)
- 字符串处理
- SH7218T拆解手记(1)拆解刷机底包
- poj 1611 The Suspects
- 使用getIdentifier()获取资源Id
- VisualC++信息安全编程(4)实现文件自我删除
- 关于青春
- 设计模式/建造模式
- Handler中Runnable 并不是新开一个线程
- centos安装php后apache无法启动
- vmware安装
- 如何高效能拆分一个字段为多行