hdu 1232 畅通工程
2015-06-29 12:39
423 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232
并查集。。。
并查集。。。
[code]#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<map> using std::cin; using std::cout; using std::endl; using std::find; using std::sort; using std::map; using std::pair; using std::vector; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) decltype((c).begin()) #define cls(arr,val) memset(arr,val,sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for (int i = 1; i <= (int)(n); i++) #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) const int N = 1010; typedef unsigned long long ull; struct UnionFind { int par , rank ; inline void init(int n) { rep(i, n) { par[i] = i; rank[i] = 0; } } inline int find(int x) { while (x != par[x]) { x = par[x] = par[par[x]]; } return x; } inline void unite(int x, int y) { x = find(x), y = find(y); if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } inline void result(int n) { int tot = 0; rep(i, n) { if (par[i] == i) tot++; } printf("%d\n", tot - 1); } }go; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif int n, m, a, b; while (~scanf("%d", &n) && n) { go.init(n); scanf("%d", &m); rep(i, m) scanf("%d %d", &a, &b), go.unite(a, b); go.result(n); } return 0; }
相关文章推荐
- 用在线的Macaulay2把曲线参数方程变成隐函数形式
- SQL Server 2014,表变量上的非聚集索引
- C++ CTreeview的checkbox使用方法
- nginx配置文件讲解
- Debian中VirtualBox增强功能的相关配置
- DataTables自定义事件
- c++正则表达式模板库GRETA的使用
- c++学习笔记4,调用派生类的顺序构造和析构函数(一个)
- 二级菜单
- 各种join一目了然: join 、inner join、left join 、right join、full join
- C#用匿名方法定义委托的实现方法
- Servle原理
- 简单数据访问类,生成简单SQL,自动转换成java对象
- DataTables给表格绑定事件
- vc中使用SendMessage自定义消息函数
- 2.IoC原理与具体使用
- Android权威官方屏幕适配全攻略
- 移动端点击穿透和click事件300ms延迟问题
- VC++多线程编程
- 下拉菜单select友情链接自动跳转做法