poj union_find相关之1611 The Suspects
来源:互联网 发布:淘宝卖家如何做好运营 编辑:程序博客网 时间:2024/06/18 06:04
poj union-find 1611 The Suspects
就是简单的并查集
#include<cstdio>#include<cstring>#include<iostream>#include<memory.h>#define MAXS 30005#define MAXG 505using namespace std;int n, m;int pre[MAXS];int find(int t){ int a = t; while (pre[a] != a) a = pre[a]; return a;}void union_int(int a,int b){ int x, y; x = find(a); y = find(b); if (x != y) pre[y] = x;}int main(){ //freopen("1.txt", "r", stdin); int i, j, k, a, b, cont; while (scanf("%d%d", &n, &m) != EOF && (n || m)) { for (i = 0; i < n; i++) pre[i] = i; for (i = 0; i < m; i++) { scanf("%d", &k); scanf("%d", &a); for (j = 1; j < k; j++) { scanf("%d", &b); union_int(a, b); } } cont = 1; j = find(0); for (i = 1; i < n;i++) if (find(i) == j) cont++; printf("%d\n", cont); }}
252K 16MS
比较高端的并查集的题 1182:食物链可以参考如下的博客:
http://blog.163.com/happyliyifan@126/blog/static/37462772201361695127793/
http://blog.csdn.net/tiantangrenjian/article/details/7085575
阅读全文
0 0
- poj union_find相关之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
- osip的一些知识
- App移动端项目管理
- centos6.9安装mysql5.7
- 16、Android 生成.so文件
- RobotStudio仿真—工作站逻辑
- poj union_find相关之1611 The Suspects
- java调用scp,文件从本地复制到远程
- [LeetCode] 554. Brick Wall
- maven资源库
- 适配器模式
- Mac下Nginx启动、重启和关闭
- Swarm、Fleet、Kubernetes、Mesos
- 过滤器Filter
- 直接插入排序