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.

InputThe 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. 
OutputFor each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks. 
Sample Input

  1. 2
  2. 5 3
  3. 1 2
  4. 2 3
  5. 4 5
  6.  
  7. 5 1
  8. 2 5

Sample Output

  1. 2
  2. 4
  3.  
  4. 这个题做麻烦了,其实可以直接遍历pre数组,其中pre[i]==i的个数就是所有集合的个数!
  1. #include<iostream>
  2. #include<string>
  3. #include<algorithm>
  4. #include<cstdio>
  5. #include<queue>
  6. #include<cstring>
  7. #include<cmath>
  8. #include<vector>
  9. #include<set>
  10. #include<iomanip>
  11. #include<iostream>
  12. using namespace std;
  13. #define MAXN 30000
  14. #define INF 0x3f3f3f3f
  15. /*
  16. 并查集
  17. */
  18. set<int> s;
  19. int pre[MAXN],n,m;
  20. int find(int x)
  21. {
  22. if(x==pre[x])
  23. return x;
  24. else
  25. return pre[x] = find(pre[x]);
  26. }
  27. void mix(int x,int y)
  28. {
  29. int fx = find(x),fy = find(y);
  30. if(fx!=fy)
  31. pre[fy] = fx;
  32. }
  33. int main()
  34. {
  35. int t;
  36. cin>>t;
  37. while(t--)
  38. {
  39. int x,y;
  40. cin>>n>>m;
  41. for(int i=;i<=n;i++)
  42. pre[i] = i;
  43. s.clear();
  44. for(int i=;i<m;i++)
  45. {
  46. cin>>x>>y;
  47. mix(x,y);
  48. }
  49. int cnt = ;
  50. for(int i=;i<=n;i++)
  51. {
  52. int fi = find(i);
  53. if(!s.count(fi))
  54. {
  55. cnt++;
  56. s.insert(fi);
  57. }
  58. }
  59. cout<<cnt<<endl;
  60. }
  61. return ;
  62. }

C - How Many Tables 并查集的更多相关文章

  1. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  2. hdu1213 How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. POJ-1213 How Many Tables( 并查集 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...

  4. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

  5. HDU 1213 How Many Tables (并查集,常规)

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  6. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  7. hdu1213 How Many Tables 并查集的简单应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...

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

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

  9. 并查集-F - How Many Tables

    F - How Many Tables 并查集的模板都能直接套,太简单不注释了,就存个代码 #include<bits/stdc++.h> using namespace std; ; i ...

随机推荐

  1. 数学 HDOJ 5301 Buildings

    题目传送门 /* 题意:n*m列的矩阵,删除一个格子x,y.用矩形来填充矩阵.且矩形至少有一边是在矩阵的边缘上. 求满足条件的矩形填充方式中面积最大的矩形,要使得该最大矩形的面积最小. 分析:任何矩形 ...

  2. HTML基础2——综合案例2——复杂的嵌套列表

    <html> <head> <title></title> </head> <body> <ul type="d ...

  3. OpenCV2.4.9 + VS2012 + win10 配置

    Step1 下载opencv 2.4.9 pack Step2 解压到本地 我解压路径是: C:\OPENCV Step3 添加环境变量: 这里虽然把X64下的VC11(VC11对应VS2012的C+ ...

  4. MySQL与Sqlserver数据获取

    由于项目要求,一个.net mvc登录注册的东西网站必须放弃sqlserver数据去使用MySQL数据库,因此我遇到了一些问题,并找出相应的解决方法, 因为sqlserver跟MySQL的数据引擎不同 ...

  5. SAS进阶《深入分析SAS》之数据汇总和展现

    SAS进阶<深入分析SAS>之数据汇总和展现 1. 通过Print过程制作报表 proc print <data=数据集>; run; 选项: obs=修改观测序号列标签 no ...

  6. css的新特性 calc () 使用

    calc()对大家来说,或许很陌生,不太会相信calc()是css中的部分.因为看其外表像个函数,既然是函数为何又出现在CSS中呢?这一点也让我百思不得其解,今天有一同事告诉我,说CSS3中有一个属性 ...

  7. clipboard.min.js 复制表格内容

    <script type="text/javascript" src="js/clipboard.min.js"></script> & ...

  8. dubbo之服务容器

    服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源. 服务容器只是一个简单的Mai ...

  9. 十年后我不会log,还是活的很好啊

    混迹于互联网也一两年了,出于喜爱与生活压力依然会从事很久.迟迟不写博客,大概是觉得知识与经验积累在笔记上时不时看看就好了,而实际情况是笔记很少翻,遇到问题搜博客和百度依然是首选,故开通博客记录自己工作 ...

  10. HDU_1207_汉诺塔2

    汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...