HDU1213 How Many Tables (并查集)
来源:互联网 发布:iphone剪辑音乐软件 编辑:程序博客网 时间:2024/06/05 07:39
题目大意:有一个人要过生日了,请他的朋友来吃饭,但是他的朋友互相认识的才能坐在一起,朋友的编号从1 ~ n,输入的两个数代表着这两个人互相认识(如果1和2认识,2和3认识,那么1和3也就认识了)。问需要多少桌子。
思路:并查集的基础题目,pre数组存的是父节点的值,root数组代表是否为根节点。最后统计根节点的数量就可以了~~~~~
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <cctype>#include <algorithm>using namespace std;const int MAXN = 1e3 + 3;int pre[MAXN];bool root[MAXN];int Find(int x){ int r = x; while(pre[r] != r) { r = pre[r]; } int i = x,j; while(pre[i] != r) //路径压缩 { j = i; i = pre[i]; pre[j] = r; } return r;}void mix(int a,int b){ int x = Find(a); int y = Find(b); if(x > y) { pre[x] = y; } if(x < y) { pre[y] = x; }}int main(){ int t; scanf("%d",&t); while(t--) { int M,N; for(int i = 1; i <= MAXN; i++) { pre[i] = i; root[i] = false; } scanf("%d%d",&M,&N); while(N--) { int a,b; scanf("%d%d",&a,&b); mix(a,b); } for(int i = 1; i <= M; i++) { if(pre[i] == i) root[i] = true; } int ans = 0; for(int i = 1; i <= M; i++) { if(root[i]) ans ++; } printf("%d\n",ans); } 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 并查集
- CF 3C Tic-tac-toe 井字棋 简洁代码
- T-SQL程序设计之运算符与表达式
- Modern UI for WPF笔记 创建空白窗口
- Mysql基本操作
- C++中对象指针总结
- HDU1213 How Many Tables (并查集)
- Python学习笔记(1)
- Android之RecyclerView使用
- 高效编程习惯:VS 自定义代码模版
- Kinds of Fuwas(暴力+组合数学)
- CPA、CPS、CPM、CPT、CPC 是什么
- hdu5112
- hdoj 1495 (bfs())(非常可乐)
- Android M新特性Doze and App Standby模式详解