HDU 1213 How Many Tables 并查集
来源:互联网 发布:工作站知乎 编辑:程序博客网 时间:2024/06/07 23:45
题意
n个点,m条边,无向图,求最后形成的集合有多少个。
解法
基础的并查集
传送门
http://acm.hdu.edu.cn/showproblem.php?pid=1213
Code
#include <cstdio>#include <iostream>using namespace std;class UnionFind {private: int * fa; int size;public: UnionFind(int n) { size = n; fa = new int[n+1]; for(int i = 1 ; i <= size ; i ++) { fa[i] = i; } } ~UnionFind() { delete[] fa; } int Find(int x) { if(fa[x] != x) { fa[x] = Find(fa[x]); } return fa[x]; } void Union(int x,int y) { int fx = Find(x); int fy = Find(y); if(fx != fy) { fa[fx] = fy; } } int GetCount() { int count = 0; for(int i = 1 ; i <= size ; i ++) { if(fa[i] == i) { count ++; } } return count; }};int main() { int caseNum; scanf("%d",&caseNum); while(caseNum--) { int n , m ; scanf("%d %d",&n,&m); UnionFind uf(n); for(int i = 1 ; i <= m ; i ++) { int u , v; scanf("%d %d",&u,&v); uf.Union(u,v); } int ans = uf.GetCount(); printf("%d\n",ans); } return 0;}
0 0
- 并查集 hdu How Many Tables
- hdu 1213 How Many Tables(并查集~~)
- Hdu 1213How Many Tables 并查集
- 杭电hdu 1213 How Many Tables并查集
- HDU 1213 How Many Tables(并查集)
- HDU 1213 How Many Tables,并查集
- HDU--1213--How Many Tables--并查集
- hdu 1213 How Many Tables(并查集练习)
- HDU 1213 How Many Tables 并查集入门
- HDU 1213 How Many Tables 并查集 水~
- HDU 1213 How Many Tables(并查集)
- [ACM] hdu 1213 How Many Tables(并查集)
- [ACM] hdu 1213 How Many Tables(并查集)
- 【并查集】 HDU 1213 How Many Tables
- hdu 1213 How Many Tables(并查集学习)
- HDU-#1213 How Many Tables (并查集)
- HDU - 1213 How Many Tables (简单并查集)
- HDU 1213 How Many Tables(并查集)
- js基础Demo
- “秦晋之好”的来历:秦晋之好与战略结盟
- salt数据系统---grains
- 如何使用Spark的local模式远程读取Hadoop集群数据
- LeetCode: 11. Container With Most Water
- HDU 1213 How Many Tables 并查集
- 通过dt筛选符合条件的结果
- BZOJ 3306: 树
- ORS Visual医学影像三维处理软件简介
- linux exercise2
- 使用maven的tomcat插件部署项目
- 设计模式之适配器模式
- Java中实现多线程下载
- HDU 4198 Quick out of the Harbour(BFS+优先队列)