HDU-1213 How Many Tables
来源:互联网 发布:股票抢单软件 编辑:程序博客网 时间:2024/05/29 04:31
这是一道并查集入门题,关于并查集,有一篇挺不错的博客 任意门
题意:安排桌子,每张桌子上只能坐彼此认识的人,输出最少安排多少张桌子
可以通过查找根节点的个数来确定
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int pre[1022];int Find(int x){ int r=x; while(r!=pre[r]) r=pre[r]; int i=x,j; //路径压缩 while(pre[i]!=r) { j=pre[i]; pre[i]=r; i=j; } return r;}void join(int x,int y){ int fx=Find(x),fy=Find(y); if(fx!=fy) { pre[fx]=fy; } else return ;}int main(){ int t,n,m; scanf("%d",&t); while(t--) { int sum=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) pre[i]=i; int a,b; while(m--) { scanf("%d%d",&a,&b); join(a,b); } for(int i=1;i<=n;i++) //判断是否为根节点 if(pre[i]==i) sum++; printf("%d\n",sum); } return 0;}
阅读全文
0 0
- 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
- hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 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
- 关于c++中的scanf函数中的一些不易发现的注意点
- selenium + python 基础
- JS函数
- 指数循环节 处理A^B 问题 Super A^B mod C + Calculation
- [STL]set实用用法
- HDU-1213 How Many Tables
- hdu 6058 并查集
- vector用法
- CTF实验吧-忘记密码了【vim编辑器备份文件】
- 通过Flow simulations找到机箱散热设计最佳方案
- JS的错误与调试
- RXD and math(HDU 6063 快速幂)
- tensorflow如何队列式同步批量读取照片(2)
- UVA 257