HDU 1213 How Many Tables 基础并查集★
来源:互联网 发布:js 截取字符串后几位 编辑:程序博客网 时间:2024/05/21 00:32
题意:
n个人参加晚宴;
完全不认识的两个人不能被分配在同一餐桌;
认识具有传递性:A认识B B认识C,那么A和C也认识.
题解:
很明显的并查集模版题.
将认识两个人合并到同一集合;
最后统计有多少个不同的集合即可;
#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<string>#include<cstring>#include<cstdio>const int maxn=100005;const int INF=0x3f3f3f3f;typedef long long LL;using namespace std;int par[maxn];int N,M;void init(){for(int i=0;i<=N;i++) par[i]=i;}int GetRoot(int a){return par[a]==a?a:par[a]=GetRoot(par[a]);}void Merge(int a,int b){int p1=GetRoot(a);int p2=GetRoot(b);if(p1!=p2) par[p2]=p1;}int main(){//freopen("E:\\ACM\\test.txt","r",stdin);int T,a,b;cin>>T;while(T--){cin>>N>>M;init();for(int i=0;i<M;i++){cin>>a>>b;Merge(a,b);}int ans=0;for(int i=1;i<=N;i++)if(par[i]==i) ++ans; //关键的两行 cout<<ans<<endl;}return 0;}
阅读全文
0 0
- HDU 1213 How Many Tables 基础并查集★
- 【hdu 1213 基础并查集】How Many Tables
- HDU 1213How Many Tables(基础并查集)
- 并查集 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(并查集学习)
- C++ lambda
- vue v-model
- C++学习之路(番外篇)
- 在linux安装JDK
- 保存文件到手机内存
- HDU 1213 How Many Tables 基础并查集★
- 截单返架系统分配货位优化
- 组合数 【nyoj-32】【stl】【dfs】
- Luogu P1563 [NOIP2016]玩具谜题
- 必备shell命令
- CentOS7 安装Mldonkey(电驴)步骤
- 相对定位 绝对定位 两张图片完全重叠的处理方法之背景图
- 从单例模式的Double-Check看指令重排
- “bc: command not found ” 解决