题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
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 55 1 2 5
Sample Output
2 4

将问题抽象出来就是并查集,就是要求进行若干次merge操作之后,还会剩下多少颗树, 这里说的树指的是假设每个人都是一棵树。拥有相同根树的人将树减一 (--count)。其实,这也是社交网络中的最基本的功能,每次系统向你推荐的那些好友一般而言,会跟你在一个“圈子”里面,换言之,也就是你可能认识的人,以并查集的视角来看这层关系,就是你们挂在同一颗树上。

如下代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define M 1000+5
using namespace std;int root[M],sz[M];
int count;int find(int p){while(p!=root[p]){root[p]=root[root[p]];/*路径压缩,会破坏掉当前节点的父节点的尺寸信息,因为压缩后,当前节点的父节点已经变了 */ p=root[p];}return p;
}void merge(int p, int q){int pRoot=find(p);int qRoot=find(q);if(pRoot == qRoot )   return ;if(sz[pRoot]<sz[qRoot]){// 按秩进行合并,将子树小的挂在子树大的上边 root[pRoot]=qRoot;sz[qRoot]+=sz[pRoot];}else{root[qRoot]=pRoot;sz[pRoot]+=sz[qRoot];}--count; // 每次合并之后,树的数量减1
}int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d%d", &count, &n);for(int i=1; i<=count; ++i){root[i]=i;sz[i]=1;}for(int i=0; i<n; ++i){int a,b;scanf("%d%d", &a, &b);merge(a, b);}printf("%d\n",count);}return 0;
}

参考资料:http://blog.csdn.net/dm_vincent/article/details/7655764

参考资料:http://blog.csdn.net/dm_vincent/article/details/7769159

HD 1213 How Many Tables(裸 并查集)相关推荐

  1. HDU 1213 How Many Tables(并查集模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...

  2. HDU 1213 How Many Tables【并查集】

    解题思路:和畅通工程类似,问最后还剩下几个不连通的区域. How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  3. PAT - L1-020. 帅到没朋友(裸并查集)

    L1-020. 帅到没朋友 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为 ...

  4. 【转】并查集MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 ...

  5. ACM: 畅通工程-并查集-解题报告

    畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description某省调查城镇交通状况, ...

  6. 【HDU - 1863】 畅通工程(并查集+最小生成树)

    题干: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的 ...

  7. - 并查集详解(第二节)

    以下是并查集思路详解: 一:概念 并查集处理的是"集合"之间的关系.当给出两个元素的一个无序数对(a,b)时,需要快速"合并"a和b分别所在的集合,这期间需要反 ...

  8. hdu 1213 How Many Tables ([kuangbin带你飞]专题五 并查集)

    点击打开链接 C - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  9. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

最新文章

  1. vue 不是内部或外部命令,也不是可运行的程序 或批处理文件
  2. 大学毕业后,我将何去何从?
  3. (011) Linux之高级键盘技巧
  4. 我的Java开发学习之旅------Java经典排序算法之二分插入排序
  5. Boost:获取随机数的实例
  6. Windows-server-2008-R2安装Oracle-11g-R2-dataguard
  7. docker重启后容器消失_docker设置固定ip地址
  8. java put set_Java PutItemRequest.setItem方法代码示例
  9. Nucleic Acids Research | NONCODE数据库V6版发布,涵盖全面的动植物长非编码RNA注释
  10. Map 遍历的几种方法
  11. 用户画像-标签体系(dwt层)
  12. DDOS 攻击工具
  13. 【验证码识别】OpenCV挑战顶象滑动拼图验证码
  14. ESP8266通过VS1838B 解码红外遥控器
  15. 长江水位查询天气查询
  16. 忘记 Apple ID 密码?重设 Apple ID 密码的 3 种方法
  17. 控制WINDOWS,使电脑说话的小玩意
  18. RTL8367学习笔记1——基础知识
  19. Java入门第三天2-java循环嵌套
  20. 微信开放平台和公众平台的区别?

热门文章

  1. html5列表去掉符号,从Python字符串中删除不在允许列表中的HTML标记
  2. php调用c语言库,使用C语言给php写扩展
  3. 单月营收超 12 亿元,叮咚买菜如何携手神策数据造就增长神话?
  4. 基于 WebSocket 的 MQTT 移动推送方案
  5. nmon_analyser 在Aix平台的使用方法
  6. Java的对象初始化过程
  7. MyBatis知多少(22)MyBatis删除操作
  8. Tricks with Direct Memory Access in Java
  9. 复数矩阵QR分解算法的C++实现
  10. subprime debt