HDU1213 How Many Tables【并查集】
来源:互联网 发布:windows2008内存优化 编辑:程序博客网 时间:2024/05/16 06:30
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1213
题目大意:
N个朋友聚会,只有认识的人才会坐在一桌。给你M个朋友关系(A,B),表示A认识B。且认识
关系具有传递性。即如果A认识B,B认识C,那么A也认识C。所以A、B、C可以坐在一桌上。
那么问题来了:问:如果让认识的人坐一桌,那么最少要安排多少张桌子才能满足要求。
思路 :
直接能想到用并查集来做。对于所给认识关系(A、B),查找二人的父节点是否相同,不相同则并
为一个集合,相同说明之前已经认识了(不做出来)。最后统计集合的个数就行了。
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int MAXN = 1100;int father[MAXN],ANS[MAXN];int Find(int x){ if(x != father[x]) father[x] = Find(father[x]); return father[x];}int main(){ int T,a,b,M,N; cin >> T; while(T--) { memset(ANS,0,sizeof(ANS)); cin >> N >> M; for(int i = 0; i <= N; ++i) father[i] = i; for(int i = 0; i < M; ++i) { cin >> a >> b; a = Find(a); b = Find(b); if(a != b) father[b] = a; } int ans = 0; for(int i = 1; i <= N; ++i) { a = Find(i); ANS[a]++; } for(int i = 1; i <= N; ++i) if(ANS[i] != 0) ans++; cout << ans << endl; } 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 并查集
- DEMON进程+inotify自动生成系统级文件操作日志demo
- 冒泡,插入,归并排序
- stringstream中的.clear()和.str()
- android ndk jni 编程入门例子
- Golang适合高并发场景的原因分析
- HDU1213 How Many Tables【并查集】
- 《HTTP权威指南》读书笔记(3)-HTTP连接管理及对TCP性能的考虑
- Apache 文件根目录设置修改方法 (Document Root)
- colinux安装
- 多进程和mmap 实现copy的demo
- Crypto++
- PHP 5.x Shellshock Exploit (bypass disable_functions)
- gopush-cluster 架构
- LA 6533 Inverting Huffman 构造+贪心