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. [Android Pro] Android中IntentService的原理及使用

    转载自:http://blog.csdn.net/ryantang03/article/details/8146154 在Android开发中,我 们或许会碰到这么一种业务需求,一项任务分成几个子任务 ...

  2. 卡方检验(Chi-square test/Chi-Square Goodness-of-Fit Test)

    什么是卡方检验 卡方检验是一种用途很广的计数资料的假设检验方法.它属于非参数检验的范畴,主要是比较两个及两个以上样本率( 构成比)以及两个分类变量的关联性分析.其根本思想就是在于比较理论频数和实际频数 ...

  3. wifiphisher使用介绍

    1.github地址:https://github.com/sophron/wifiphisher 2.需要安装在kali linux下面 3.需要两个无线网卡 4.安装方法是使用介绍,参考githu ...

  4. 寻路DEMO

    网格场景的寻路算法DEMO 灰色格子为默认正常蓝色格子表示为障碍物,不可进入红色细条在格子周围,表示格子的墙,用于阻碍行走紫色为当前鼠标选中格子黄色为寻路的起始位置和结束位置.鼠标左键设置,CTRL+ ...

  5. unity 静态合批

    想做这样一个优化 因为cmd drawcall太多 materials太多导致 实际上只是贴图不一样 想用texture2DArray把他们合起来 texArray这步功能倒是很快就好了 但是从fra ...

  6. RichTextBox 清空

    this.tbContent.Document.Blocks.Clear();

  7. (回溯法)和为n的所有不增正整数和式分解算法

    题目: 利用递归算法输出正整数和为n的所有不增的正整数和式.例如当n=5时,不增的和式如下: 5=5 5=4+1 5=3+2 5=3+1+1 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 ...

  8. Solr打分出错

    solr支持给某Field打分,在验证的过程的过程中出现错误:ERROR: [doc=likehua] cannot set an index-time boost, unindexed or nor ...

  9. PHPCMS部件

    数据库操作: $member_db = pc_base::load_model('member_model'); $memberinfo = $member_db->get_one(array( ...

  10. VB.NET版机房收费系统---外观层怎样写

    外观设计模式.<大话设计模式>第103页具体解说,不记得这块知识的小伙伴能够翻阅翻阅,看过设计模式,敲过书上的样例,仅仅是学习的第一步,接着,假设在我们的项目中灵活应用,把设计模式用出花儿 ...