专题四 · 1003
来源:互联网 发布:手机怎样备份所有数据 编辑:程序博客网 时间:2024/05/19 22:46
代码及解释
#include <algorithm>#include <cmath>#include <cstdio>#include <iostream>// 上课讲的一道题// 并查集的思考:// 一种并查集是上课的时候老师讲的// 这种并查集的查询效率很快// 但是更新的速度比较慢// 实际上另外一种是查询效率慢// 更新速度很快// 这两种可以应用于不同的场景using std::cin;using std::cout;using std::endl;const int SIZE = 1500;int set[SIZE];int find_set(int x) { int r = x; while (set[r] != r) r = set[r]; return r;}void merge(int x, int y) { x = find_set(x); y = find_set(y); if (x != y) set[x] = y;}int main() { int n, m; while (cin >> n && n) { for (int i = 1; i <= n; i++) set[i] = i; cin >> m; while (m--) { int x, y; scanf("%d %d", &x, &y); merge(x, y); } int count = -1; for (int i = 1; i <= n; i++) if (set[i] == i) count++; printf("%d\n", count); }}
0 0
- 专题四 · 1003
- 专题四1003
- 专题四1003
- 1003-专题四
- 专题四-1003
- 专题四 · 1001
- 专题四 · 1002
- 专题四 · 1004
- 专题四 · 1006
- 专题四 · 1005
- 专题四 · 1008
- 专题四 · 1009
- 专题四 · 1010
- 专题四 · 1011
- 2016sdau课程练习专题四 1003
- 3G专题四
- LDAP专题(四)
- 专题四 Problem A
- iOS-图片倒圆角的三种方式
- ORACLE——备份
- spark 学习笔记1
- cookie和session的区别及session的生命周期
- 设计模式之模板方法模式
- 专题四 · 1003
- c++类成员函数作为回调函数
- JAVA多线程实现的三种方式
- 获取路径
- Tarball-百度百科
- winpcap实现syn攻击
- css 比较 在同一个水平线上
- MapReduce任务运行到running job卡住
- Hadoop经典案例Spark实现(一)——通过采集的气象数据分析每年的最高温度