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 55 1
2 5

Sample Output

2
4

解法分析及AC代码


此题为并查集的模板题,只要找到不同集合的个数再计数即可,具体见代码及注释。

#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
int t,n,m,total;
int a[maxn],b[maxn];//数组a存入的是每个人所属的集合的名称int find(int x) //这个函数用来找到自己所属的集合,返回的是自己所属集合的名称
{return x==a[x]?x:a[x]=find(a[x]);//使用路径压缩
}
void sett(int n)//初始化将一开始每个人所属的集合的名称定义成自己的序号
{for(int i=1;i<=n;i++){a[i]=i;}
}void sum(int x,int y)//此函数用来合并两个朋友的所属集合,使它们在一个集合里面
{if(x!=y) a[x]=y;
}
int main()
{scanf("%d",&t);while(t--){memset(b,0,sizeof(b));total=0;scanf("%d%d",&n,&m);sett(n);for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);sum(find(x),find(y));}for(int i=1;i<=n;i++){//在同一个集合中的人共用一张桌子,一个集合只被计数一次,下面是计数一次的方法if(b[find(a[i])]++==0) total++;}printf("%d\n",total);}
}

HDU1213How Many Tables相关推荐

  1. FLUSH TABLES WITH READ LOCK

    1.FLUSH TABLES WITH READ LOCK 这个命令是全局读锁定,执行了命令之后所有库所有表都被锁定只读.一般都是在数据库联机备份,这个时候数据库的写操作将被阻塞,读操作顺利进行. 解 ...

  2. R创建透视表(Pivot Tables)

    R创建透视表(Pivot Tables) 目录 R创建透视表(Pivot Tables) 透视表(Pivot Tables) R创建透视表

  3. mysqldump备份数据库时出现when using LOCK TABLES

    用mysqldump备份数据库时,如果出现when using LOCK TABLES,解决办法是加上 --skip-lock-tables 例如: 用mysqldump备份数据库时出现 29: Fi ...

  4. FLUSH TABLES WITH READ LOCK 和 LOCK TABLES

    今天在解决数据库同步异常的时候用到了flush tables with read lock 这个命令,于是顺便就学习了下锁表的相关知识. 1.FLUSH TABLES WITH READ LOCK 这 ...

  5. hdu 1213 How Many Tables ([kuangbin带你飞]专题五 并查集)

    点击打开链接 C - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  6. Partition Tables介绍及分区表转换

    2019独角兽企业重金招聘Python工程师标准>>> --==================== -- Partition Tables --================== ...

  7. 转 plsql dev中Dynamic Performance Tables not accessible分析解决

    相信很多使用plsql dev的朋友多遇到过类此如下面的提示: Dynamic Performance Tables not accessible, Automatic Statistics Disa ...

  8. Mysql报错Fatal error: Can#39;t open and lock privilege tables: Table #39;mysql.host#39; doesn#39;t...

    安装mysql后,启动时候没有启动成功,查看了下日志报错如下:--------------------------------------------- 1   可以:初始化mysql:mysql_i ...

  9. parfile解决exp时tables过多问题

    parfile解决exp时tables过多问题 参考文章: (1)parfile解决exp时tables过多问题 (2)https://www.cnblogs.com/tzhyy/p/10469333 ...

最新文章

  1. scala集合fold实例:将两个Map合并
  2. 了解 C# foreach 内部语句和使用 yield 实现的自定义迭代器
  3. java支付模块架构,涨薪7K!
  4. 第二天一早的飞鸽传书
  5. dhcp snooping华为_使用DHCP snooping 功能防止DHCP Server仿冒者攻击(华为交换机)
  6. JavaScript 工作必知(九)function 说起 闭包问题
  7. STM32之点亮LED
  8. [转载] Pytorch基础介绍
  9. 成功解决 遇到3dmax模型文件出错,找回并修复出错文件
  10. Firefox版哔哩哔哩助手,修改自chrome1.2.1版
  11. poj:2455 Secret Milking Machine 秘密挤奶机(二分+最大流)
  12. 基于Pytorch实现自建数据库的深度神经网络模型案例
  13. Lanbda表达式 java8新特性
  14. IDEA中配置Vue启动Configurations
  15. 中望CAD教程:如何将PDF转化为DWG格式文件
  16. 微信公众号:支付宝支付
  17. Cesium ClippingPlane剖切 改造 限高分析
  18. Android桌面角标调研
  19. 1分钟免费开通IDaaS云服务
  20. 用java实现字符串的加密_JAVA 字符串加密、密码加密实现方法

热门文章

  1. tar命令解压jdk.tar.gz包 报错 gzip: stdin: not in gzip format
  2. 虚拟化技术(1)——介绍
  3. Linux系统调用getuid的简单分析
  4. 红旗与火狐携手打造优质开源体验,推出红旗Linux专版火狐浏览器
  5. 普通账户信息是SIEM的盲点
  6. str split函数 php,怎么在php中利用str_split函数分割字符串
  7. swing怎么监听其他类的按钮_舍得酒因资金违规占用被ST,“其他类风险”爆雷到底怎么防?...
  8. gorilla websocket无法跨域_聊聊浏览器同源策略与跨域方案详解
  9. Django项目:CMDB(服务器硬件资产自动采集系统)--11--07CMDB文件模式测试采集硬件数据...
  10. day18-事务与连接池 1.复习