HDU 1213 How Many Tables (并查集)
How Many Tables
题目链接:
http://acm.hust.edu.cn/vjudge/contest/123393#problem/C
Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input
2
5 3
1 2
2 3
4 5
5 1
2 5
Sample Output
2
4
题意:
n个人参加晚宴;
完全不认识的两个人不能被分配在同一餐桌;
认识具有传递性:A认识B B认识C,那么A和C也认识.
题解:
很明显的并查集模版题.
将认识两个人合并到同一集合;
最后统计有多少个不同的集合即可;
注意:部分编译器不允许变量名与关键字冲突;故注意next和rank这些关键字,以免CE.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1100
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int fa[maxn];
int _rank[maxn];
void init_set() {
for(int i=0; i<maxn; i++) {
fa[i] = i;
_rank[i] = 0;
}
}
int find_set(int x) {
return fa[x] = (x==fa[x]? x:find_set(fa[x]));
}
void unit_set(int x, int y) {
x = find_set(x);
y = find_set(y);
if(_rank[x] < _rank[y]) swap(x, y);
fa[y] = x;
if(_rank[x] == _rank[y]) _rank[x]++;
}
int n, m;
int main(int argc, char const *argv[])
{
//IN;
int t; cin >> t;
while(t--)
{
cin >> n >> m;
init_set();
while(m--) {
int x,y; scanf("%d %d", &x, &y);
unit_set(x,y);
}
int cnt = 0;
bool ans[maxn] = {0};
for(int i=1; i<=n; i++) {
ans[find_set(i)] = 1;
}
for(int i=1; i<=n; i++)
if(ans[i]) cnt++;
printf("%d\n", cnt);
}
return 0;
}
HDU 1213 How Many Tables (并查集)的更多相关文章
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...
- HDU 1213 How Many Tables (并查集,常规)
并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- hdu 1213 求连通分量(并查集模板题)
求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- HDU - 1213 How Many Tables 【并查集】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...
随机推荐
- RecyclerView(5)官方教程带简单示例
Create Lists The RecyclerView widget is a more advanced and flexible version of ListView. This widge ...
- SharePoint2010安装文档
最低配置要求 操作系统 X64 Window Server 2008 R2 Standard 数据库 X64 SQL Server 2008 R2 CPU X64,4核 内存 8G 硬盘 100G 安 ...
- HDU 1711 (裸KMP) Number Sequence
题意: 用第二个数列去匹配第一个数列,输出第一次匹配到的位置,如果没有则输出-1. 分析: 这明显是一道裸的KMP. 我是在这篇博客上学的KMP算法的,讲得比较透彻. http://blog.csdn ...
- bzoj1834: [ZJOI2010]network 网络扩容
努力看了很久样例一直过不了...然后各种输出中间过程啊巴拉巴拉弄了1h,没办法了...然后突然想到啊原来的边可以用啊为什么不用...于是A了...感人肺腑 #include<cstdio> ...
- Selenium Tutorial (2) - Selenium IDE In Depth
Installing Firefox and Firebug Installing and Opening Selenium IDE Starting with test cases and test ...
- ios tweak之binary not signed (use ldid -S)问题解决
参考tweak教程写了个简单的tweak,无奈完全无效果,摸索了好长时间才找到方法: 打开terminal ssh root@192.168.1.100 vim /var/log/syslog 找到如 ...
- 【转 iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
原文网址:http://blog.csdn.net/pucker/article/details/41843511 上一篇文章<iOS 8界面自动布局系列-1>简要介绍了iOS界面布局方式 ...
- JOB的创建,定时,执行
--建表 create table test_job(para_date date); commit; insert into test_job values(sysdate); commit; ...
- 常用UI布局
1.LinearLayout(线性布局):将布局所包含的控件在线性方向上依次排列. <1>android:orientation 指定了排列方向(垂直方向(vertical).水平方向(h ...
- Android Studio进行NDK编程