HDU_1232_畅通工程
来源:互联网 发布:防止mysql 注入攻击 编辑:程序博客网 时间:2024/05/22 06:38
HDU 1232
思路:最基础的并查集,直接写就可以,最后看看总共有几个集合,则输出总集合个数-1就是需要修的路数。
#include <iostream>#include <iomanip>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#include <set>#include <map>#include <list>#include <stack>#include <deque>#include <queue>#include <vector>#include <algorithm>#include <functional>#define debug(x) cout << "--------------> " << x << endlusing namespace std;const double PI = acos(-1.0);const double eps = 1e-10;const long long INF = 0x7fffffff;const long long MOD = 1000000007;const int MAXN = 1000 + 7;int n, m;int pre[MAXN];int findRootNode(int x){ int r = x; while(pre[r] != r) r = pre[r]; int i = x; //路径压缩 while(i != r) { int j = pre[i]; pre[i] = r; i = j; } return r;}void setMerge(int x, int y){ int root_x, root_y; root_x = findRootNode(x); root_y = findRootNode(y); if(root_x < root_y) pre[root_y] = root_x; else pre[root_x] = root_y;}int main(){ while(~scanf("%d", &n)) { if(n == 0) break; scanf("%d", &m); memset(pre, 0, sizeof(pre)); for(int i = 1; i <= n; ++i) pre[i] = i; int city_A, city_B; for(int i = 1; i <= m; ++i) { scanf("%d%d", &city_A, &city_B); setMerge(city_A, city_B); } int ans = -1; for(int i = 1; i <= n; ++i) { if(findRootNode(i) == i) ans++; } printf("%d\n", ans); } return 0;}
0 0
- HDU_1232_畅通工程
- Graph Theory NO.6 HDU_1232_畅通工程_并查集
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- 畅通工程
- Opencv中使用Rect的函数创建按钮和文字
- APUE笔记---第四章Linux文件系统与文件目录操作
- MySQL 新建数据库、建立索引、建立外键
- 傅里叶分析
- java语言中IO流知识
- HDU_1232_畅通工程
- PHP魔术方法之__call与__callStatic方法
- 在Windows上玩TensorFlow(一)——安装Docker
- 435. Non-overlapping Intervals
- Android IPC之 Messager
- HDU 2859 Phalanx 递推
- JAVA自定义注解使用
- Codeforces725D-Contest Balloons(优先队列+模拟)
- [APUE chapter 12] 线程控制