HDU1213 How Many Tables - 并查集
来源:互联网 发布:数控程序员考试 编辑:程序博客网 时间:2024/04/30 08:49
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1213
/*HDU1213 How Many Tableshttp://acm.hdu.edu.cn/showproblem.php?pid=1213*/#include <iostream> #include <stdio.h> #include <algorithm> #include <memory.h> #include <set>#include <vector>using namespace std;#define N 1005int t;int n, m;int fa[N];int find(int x){ if (x == fa[x]) return x; return fa[x] = find(fa[x]);}void merge(int x, int y){ fa[find(y)] = find(x);}set<int> sset;vector<int> v;int main(){ //freopen("in", "r", stdin); int i, j; scanf("%d", &t); for (j = 0; j < t;j++) { scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) { fa[i] = i; } sset.clear(); int a, b; for (i = 0; i < m; i++) { scanf("%d%d", &a, &b); if (find(a) != find(b)) merge(a, b); } for (i = 1; i <= n; i++) { sset.insert(find(i)); } int len = sset.size(); char s[1]; //处理中间的空格行 if (j != t - 1) gets(s); v.push_back(len); } for (i = 0; i < t; i++) { printf("%d\n", v[i]); } return 0;}
0 0
- 【hdu1213】How Many Tables【并查集】
- How Many Tables hdu1213 并查集
- hdu1213(How Many Tables 并查集)
- Hdu1213 - How Many Tables - 并查集
- HDU1213 How Many Tables 【并查集】
- HDU1213 How Many Tables【并查集】
- HDU1213 How Many Tables 【并查集】
- hdu1213 How Many Tables(并查集)
- How Many Tables--hdu1213(并查集)
- HDU1213-How Many Tables 并查集
- HDU1213 How Many Tables - 并查集
- hdu1213 How Many Tables 并查集
- HDU1213 How Many Tables(并查集)
- HDU1213 How Many Tables 并查集
- hdu1213 How Many Tables 并查集
- HDU1213 How Many Tables(并查集)
- hdu1213 How many tables 并查集
- HDU1213 How Many Tables 并查集
- HDU-2553-N皇后问题
- 算法实现一行数据左边奇数右边偶数
- 顺序表
- 从零开始学Java之单例模式:懒汉模式和饱汉模式
- 关于/etc/hosts文件
- HDU1213 How Many Tables - 并查集
- Mac实用工具
- vmware11.1.2里安装hyper-v
- (2)如何在Javascript中自定义类 & 面向对象编程 (封装)
- 在线调试微信打开的HTML5页面
- C潜规则之保持语法简单
- nginx+nginx-rtmp-module部署直播服务器
- setCheckable和setChecked的属性
- 什么是4K电视