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. linux下so获得自己文件位置的路径

    打开这个设备/proc/self/maps 返回的就是这个进程当前使用的so列表 cat /proc/self/maps00400000-0040b000 r-xp 00000000 08:01 14 ...

  2. OpenShift上部署Redis主从集群

    客户有部署有状态服务的需求,单机部署模式相对简单,尝试一下集群部署. 关于Redis的master,slave 以及sentinal的架构和作用不提,有兴趣可以参考之前的博客 https://www. ...

  3. 最小二乘法least square

    上研究生的时候接触的第一个Loss function就是least square.最近又研究了一下,做个总结吧. 定义看wiki就够了.公式如下 E(w)=12∑n=1N{y−xWT}2E(w)=12 ...

  4. 基于at91rm9200的i2c分析(DS1307实时时钟芯片)

    board-ek.c 构造i2c_board_info结构体 static struct i2c_board_info __initdata ek_i2c_devices[] = {     {    ...

  5. 你可能不知道的5 个强大的HTML5 API 函数

    HTML5提供了一些非常强大的JavaScript和HTML API,来帮助开发者构建精彩的桌面和移动应用程序.本文将介绍5个新型的API,希望对你的开发工作有所帮助. 1.  全屏API(Fulls ...

  6. 一个巧妙的方法实现elementUI的table的行选中

    问题背景:点击上面的框,选中下面对象的行数据 刚开始考虑使用的是table的事件:toggleRowSelection,但是发现一个奇怪的现象 <div v-if="orderData ...

  7. go语言string、int、int64互相转换

    #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, 6 ...

  8. 关于PHP写的投票网站之刷票终结版

    告诉大家一个坏消息,还是有刷票的行为,有图有真相: 倒数第三项是时间轴,倒数第二项是IP,倒数第一项是邮箱,你们要刷票,拦都拦不住呀呀呀呀呀呀!!!!! 看看这些时间的间隔,看看这些IP的地址,哎,再 ...

  9. [NPM] Execute npx commands with $npm_ Environment Variables

    We will incorporate npm specific environment variables when executing various npx commands. In our e ...

  10. CentOS6.3 安装配置 ant

    OS:CentOS6.3 ant版本:apache-ant-1.9.2-bin 第1步:下载ant apache-ant-1.9.2-bin.tar.gz 第2步:解压 tar -zxvf apach ...