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
 
代码:
并查集,模板的运用..
     #include<stdio.h>
#include<string.h>
#define maxn 1005
int father[maxn+],rank[maxn+]; /*³õʼ»¯*/
void init(int n)
{
for(int i=;i<=n;i++)
{
father[i]=i;
rank[i]=;
}
} /*²é*/
int setfind(int x)
{
if(father[x]==x)
return x;
return setfind(father[x]);
} /*²¢*/
void Union(int a,int b)
{
int x=setfind(a);
int y=setfind(b);
if(x!=y)
{
if(rank[x]>rank[y])
{
father[y]=x;
rank[x]+=rank[y];
}
else
{
father[x]=y;
rank[y]+=rank[x];
}
}
} int main()
{
int t,m,n,i,a,b;
/* freopen("test.in","r",stdin); freopen("test.out","w",stdout);
*/
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init(n);
for(i=;i<m;i++)
{
scanf("%d%d",&a,&b);
Union(a,b);
}
int cnt=;
for(i=;i<=n;i++)
if(father[i]==i)
cnt++;
printf("%d\n",cnt);
}
return ;
}

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. matlab 文件遍历

    function MyGetFiles(F)path=F;filelist=dir(fullfile(path,'*.JPG'));n=length(filelist);for i=1:n f=str ...

  2. [ GIT ] GIT tip : A simple .gitconfig file

    reference : http://fle.github.io/git-tip-a-simple-gitconfig-file.html As several friends have asked ...

  3. C#读写txt文件的两种方法介绍[转]

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  4. WF4.0(2)----设计工作流

    自从做了程序员,发现自己长胖了,而且自己的身体抵抗力也出了问题,最近身体不适,公司工作任务最近也很赶,上次写了WF4.0的简介,这次就工作中工作流设计的几种方式稍微总结一下.设计工作流包括四种方式:流 ...

  5. Python 批量修改图片格式和尺寸

    公司的一个项目要求把所有4096x4096的图片全部转化成2048x2048的图片,这种批量转换图片大小的软件网上很多,我的同事原来使用的美图看看的批量转换,但是稍微有点麻烦,每次还需要指定要转换的图 ...

  6. linux文件系统命令(1)---概述

    一.目的 本系列博文将介绍linux文件系统相关的命令. linux文件系统分为4个层面:用户空间程序.系统调用.内核虚拟文件系统以及实际文件系统.本系列文章仅仅涉及用户空间程序的操作及用法.旨在掌握 ...

  7. scala里的模式匹配和Case Class

    模式匹配的简介 scala语言里的模式匹配可以看作是java语言中switch语句的改进. 模式匹配的类型 包括:常量模式.变量模式.构造器模式.序列模式.元组模式以及变量绑定模式等. 常量模式匹配 ...

  8. 关于ARM的B,BL跳转指令

    .text:00001260 A3 04 00 EB                             BL      sub_24F4 当前地址+ (偏移 << 2 + 8) =  ...

  9. Postfix接收邮件后转向运行特定的脚本

    本文主要參考:http://serverfault.com/questions/258469/how-to-configure-postfix-to-pipe-all-incoming-email-t ...

  10. MAC下PHP开发

    ZendStudio 10.5安装: http://blog.sina.com.cn/s/blog_7c8dc2d50101nhvb.html PHP+MySQL+Apache开发环境安装:XAMPP ...