并查集:HDU1213-How Many Tables(并查集最简单的应用)
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31632 Accepted Submission(s): 15706
Problem 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
解题心得
- 题意就是给你一些关系,求有多少个相互之间没有关系的集合。
- 这是一个很简单的并查集的应用,直接将该合并的合并,然后直接找根,有多少个不同的根就有需要多少个桌子。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int father[maxn];
int n,m;
int find(int x)
{
if(father[x] == x)
return x;
else
return father[x] = find(father[x]);
}
void merge(int a,int b)
{
int fa = find(a);
int fb = find(b);
if(fa != fb)
father[fa] = fb;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int ans = 0;
for(int i=0;i<maxn;i++)
father[i] = i;
scanf("%d%d",&n,&m);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(find(a) != find(b))//两个之间是朋友则直接合并
{
merge(a,b);
}
}
for(int i=1;i<=n;i++)
if(father[i] == i)//在n个人中有多少个没有联系的集合就是求有多少个根
ans ++;
printf("%d\n",ans);
}
}
并查集:HDU1213-How Many Tables(并查集最简单的应用)的更多相关文章
- hdu1213 How Many Tables(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu1213 How Many Tables 并查集的简单应用
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...
- 并查集-F - How Many Tables
F - How Many Tables 并查集的模板都能直接套,太简单不注释了,就存个代码 #include<bits/stdc++.h> using namespace std; ; i ...
- 并查集——poj1703(带权并查集入门)
传送门:Find them, Catch them 题意:警察抓获N个罪犯,这些罪犯只可能属于两个团伙中的一个,现在给出M个条件(D a b表示a和b不在同一团伙),对于每一个询问(A a b)确定a ...
- windows+mysql集群搭建-三分钟搞定集群
注:本文来源: 陈晓婵 < windows+mysql集群搭建-三分钟搞定集群 > 一:mysql集群搭建教程-基础篇 计算机一级考试系统要用集群,目标是把集群搭建起来,保证一 ...
- Mongodb集群搭建之 Sharding+ Replica Sets集群架构(2)
参考http://blog.51cto.com/kaliarch/2047358 一.概述 1.1 背景 为解决mongodb在replica set每个从节点上面的数据库均是对数据库的全量拷贝,从节 ...
- 收到西门子发来的UG告知函怎么办?Solidworks盗版被查如何防范?厂商是怎么样查到公司在用盗版,有什么方法可以核实真假?……
收到西门子发来的UG告知函怎么办?Solidworks盗版被查如何防范?厂商是怎么样查到公司在用盗版,有什么方法可以核实真假?--很多企业信息化管理leader或者老板都希望能够通过一些取巧的办法来防 ...
- 搭建高可用mongodb集群(三)—— 深入副本集内部机制
在上一篇文章<搭建高可用mongodb集群(二)—— 副本集> 介绍了副本集的配置,这篇文章深入研究一下副本集的内部机制.还是带着副本集的问题来看吧! 副本集故障转移,主节点是如何选举的? ...
- mongodb的分布式集群(4、分片和副本集的结合)
概述 前面3篇博客讲了mongodb的分布式和集群,当中第一种的主从复制我们差点儿不用,没有什么意义,剩下的两种,我们不论单独的使用哪一个.都会出现对应的问题.比較好的一种解决方式就是.分片和副本集的 ...
- Apache shiro集群实现 (八) web集群时session同步的3种方法
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
随机推荐
- C 碎片一 计算机知识
一.计算机知识 1, 计算机组成及工作原理 计算机是硬件和软件的结合体.硬件由主机箱和外部设备组成,主机主要包括CPU.内存.主板.硬盘.光驱.各种扩展卡.连接线.电源等:外部设备包括鼠标.键盘等.软 ...
- 使用SpringSession管理分布式系统的会话Session
在我方供应链项目分布式部署的环境下,需要在统一网关服务中管理访问的Session,即无论访问请求路由到哪一个网关服务环境,使用的都是相同的HttpSession,这样就保证了在用户登录之后,能够使用统 ...
- Android-->RxJava2更新体验
截止日前最新版2017-3-15: RxJava compile ‘io.reactivex:rxjava:’ compile ‘io.reactivex:rxandroid:’ RxJava2 co ...
- [:space:]的用法(转)
转自:http://blog.itpub.net/27181165/viewspace-1061688/ 在linux中通常会使用shell结合正则表达式来过滤字符,本文将以一个简单的例子来说明+,* ...
- LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- 访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”的解决方案
访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”的解决方案: 打开客戶端浏览器--工具---internet-安全-自定义级别-选择到低到中低. 然后点受信任站点,把你要访问的站点 ...
- JS encodeURIComponent函数
为了避免歧义,可以用JS 的encodeURIComponent函数 将有歧义的字符(?+=等)转换成对应的ASCII编码 for(var i=0;i<whichform.elements.l ...
- IOS 某个控件出不来原因(经验分享)
某个控件出不来:(检查原因) 1.frame的尺寸和位置对不对 2.hidden是否为YES 3.有没有添加到父控件中 4.alpha 是否 < 0.01 5.被其他控件挡住了 6.父控件的前面 ...
- hdu-3371 Connect the Cities---kruskal
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目大意: 给n个城市,m条路,k组已知路,求最小费用联通所有城市: 解题思路: kruska ...
- iOS 制作表格 (数据源控制行,列数)
记得去年面试的过程中,有一个面试官问我怎么制作表格.由于之前也没有做过,当时有点懵逼,今天想起来了,就用tableview制作了一个,望不要有人像我一样掉坑了, 直接上代码: // // ViewCo ...