并查集
来源:互联网 发布:中级java程序员要求 编辑:程序博客网 时间:2024/06/06 04:34
hdu1232 畅通工程(并查集入门)
当并查集模板用
有一篇超赞的文章:http://blog.csdn.net/dellaserss/article/details/7724401
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <algorithm>#include <cmath>#include <string>#include <queue>#include <set>#include <vector>#include <cmath>using namespace std;const int M = 1000000 * 5 + 50;int pre[1500],vis[1500];int findl(int x){ int r = x; while(r != pre[r])r = pre[r]; int j = x; while(pre[j] != r) { j = pre[j]; pre[j] = r; } return r;}void Max_(int a,int b){ a = findl(a); b = findl(b); if(a != b) { pre[a] = b; }}int main(){// freopen("in.txt","r",stdin); int n,m; while(~scanf("%d",&n) && n) { for(int i = 1; i <= n; i++) pre[i] = i; scanf("%d",&m); int a,b; for(int i = 0; i < m; i++) { scanf("%d%d",&a,&b); Max_(a,b); } map<int,int>Map; for(int i = 1; i <= n; i++) { Map[findl(i)] ++; } printf("%d\n",Map.size() - 1); } return 0;}
0 0
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- 并查集
- NSValue
- jdbc数据库的连接
- ocp-010
- 2016 微软秋招(校招)在线笔试 题目1 : Farthest Point
- UI_UITableView界面传值(将前面的属性传值和协议传值结合起来)
- 并查集
- ocp-11
- 2016 微软秋招(校招)在线笔试 题目234
- 剑指offer—合并两个排序的链表
- 制作网页2---PHP学习拾遗
- 观影有感
- HDU 2082 母函数
- 第二十四篇:缩合练习代码---简单微博(自定义Cell)
- JavaScript高级程序设计之函数表达式之闭包之内存泄漏第7.2.3讲笔记