How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9828    Accepted Submission(s): 4872

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
 
Author
Ignatius.L
 
Source
 
代码:
并查集,模板的运用..
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define maxn 1005
  4. int father[maxn+],rank[maxn+];
  5.  
  6. /*³õʼ»¯*/
  7. void init(int n)
  8. {
  9. for(int i=;i<=n;i++)
  10. {
  11. father[i]=i;
  12. rank[i]=;
  13. }
  14. }
  15.  
  16. /*²é*/
  17. int setfind(int x)
  18. {
  19. if(father[x]==x)
  20. return x;
  21. return setfind(father[x]);
  22. }
  23.  
  24. /*²¢*/
  25. void Union(int a,int b)
  26. {
  27. int x=setfind(a);
  28. int y=setfind(b);
  29. if(x!=y)
  30. {
  31. if(rank[x]>rank[y])
  32. {
  33. father[y]=x;
  34. rank[x]+=rank[y];
  35. }
  36. else
  37. {
  38. father[x]=y;
  39. rank[y]+=rank[x];
  40. }
  41. }
  42. }
  43.  
  44. int main()
  45. {
  46. int t,m,n,i,a,b;
  47. /* freopen("test.in","r",stdin);
  48.  
  49. freopen("test.out","w",stdout);
  50. */
  51. scanf("%d",&t);
  52. while(t--)
  53. {
  54. scanf("%d%d",&n,&m);
  55. init(n);
  56. for(i=;i<m;i++)
  57. {
  58. scanf("%d%d",&a,&b);
  59. Union(a,b);
  60. }
  61. int cnt=;
  62. for(i=;i<=n;i++)
  63. if(father[i]==i)
  64. cnt++;
  65. printf("%d\n",cnt);
  66. }
  67. return ;
  68. }

HDUOJ---1213How Many Tables的更多相关文章

  1. HDU——1213How Many Tables(并查集按秩合并)

    J - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  2. 【HDUOJ】1213 How many tables

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意:Ignatius邀请了n个朋友来家里,朋友之间如果互相不认识的不想坐一起,所以至少要准备几 ...

  3. LOCK TABLES和UNLOCK TABLES与Transactions的交互

    LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...

  4. 函数的使用顺序---TABLES,USING,CHANGING

    SAP使用PERFORM的时候: ... [TABLES   itab1 itab2 ...]     [USING    a1 a2 ...]     [CHANGING a1 a2 ...]. E ...

  5. Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement

    Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...

  6. mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts' when using LOCK TABLES

    AutoMySQLBackup备份时,出现mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@' ...

  7. Neutron 理解 (4): Neutron OVS OpenFlow 流表 和 L2 Population [Netruon OVS OpenFlow tables + L2 Population]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  8. Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)

    传送门 Description People do many crazy things to stand out in a crowd. Some of them dance, some learn ...

  9. mysql [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist (转载)

    mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 2013-11-2 ...

  10. MySql: show databases/tables use database desc table

    1. show databases mysql> show databases;+--------------------+| Database |+--------------------+| ...

随机推荐

  1. [Android Pro] Swift 3.0多线程

    本文只介绍Grand Central Dispath(GCD) 中央调度 个人认为一个GCD就够用了,可能是改版或是其他的在找之前写的多线程方法时发现不能用了,看文档之后发现改了,现在看上去更加简单易 ...

  2. Objective-C:深复制(拷贝)

    深复制:复制对象时,如果对象中包含对象类型的实例变量,要对对象类型的实例变量也要做对象复制.新对象中的对象类型实例变量和旧对象中的对象类型实例变量指的是不同的对象.不管任何一方实例变量对对象做修改,都 ...

  3. 原创D3D几何实例化的DEMO

    CUBE的几何实例化DEMO 鼠标右键按下并拖动         旋转视角WSAD                         前后左右RF                             ...

  4. iOS开发-xCode6(iOS 8)中应用程序图标和启动页面设置

    iOS8中设置应用程序图标跟之前没有什么变化,命名规则不变,不过至于设置启动页面,网上给的方式很多都是模棱两可的,东平西凑总算是把启动页面的图片设置成功了,iOS设置启动图片有两种方式一种是Launc ...

  5. 使用jQuery在上传图片之前实现缩略图预览

    使用jQuery在上传图片之前实现缩略图预览 jQuery代码 01 $("#uploadImage").on("change", function(){ 02 ...

  6. HTML标签 闭合还是不闭合?

    你在写 HTML5 代码的时候,是否纠结过应该写 <br /> 还是 <br>,是写 <input /> 还是写 <input>.写 <scrip ...

  7. 使用Kindeditor上传图片

    给客户制作的项目中需要添加富文本,从网上看了一下很多人推荐kindeditor这个编辑器,用了之后也感觉不错,有一些问题的就是上传图片的时候遇到了一些问题,在这里记录一下,也方便以后查看. 首先在官网 ...

  8. 使用ViewDragHelper打造属于自己的DragLayout(抽屉开关 )

    使用ViewDragHelper打造属于自己的DragLayout(抽屉开关 ) DrawLayout这个自己定义的空间非经常见.qq,网易新闻.知乎等等,都有这样的效果,那这样的效果是如何实现的呢? ...

  9. [置顶] hdu 4418 高斯消元解方程求期望

    题意:  一个人在一条线段来回走(遇到线段端点就转变方向),现在他从起点出发,并有一个初始方向, 每次都可以走1, 2, 3 ..... m步,都有对应着一个概率.问你他走到终点的概率 思路: 方向问 ...

  10. visual studio2013 php

    C:\Users\Administrator\AppData\Local\Microsoft\VisualStudio\12.0\Extensions\DEVSENSE\PHP Tools for V ...