How Many Tables

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

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

杭电ACM省赛集训队选拔赛之热身赛

 

思路:并查集,统计集合个数

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std; const int maxn=1000+5;
int p[maxn];
int n,m;
int ans; void make_set()
{
for(int i=0;i<=n;i++)
p[i]=i;
} int find_set(int x)
{
return p[x]!=x ? p[x]=find_set(p[x]) : x;
} void union_set(int x, int y)
{
int fx=find_set(x), fy=find_set(y);
if(fx!=fy)
{
p[fx]=fy;
}
} int main()
{
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
make_set();
for(int i=0;i<m;i++) {
int x,y; scanf("%d%d", &x, &y);
union_set(x, y);
}
ans=0;
for(int i=1;i<=n;i++) if(p[i]==i) ans++;
printf("%d\n", ans);
} return 0;
}

hdu1213 How Many Tables的更多相关文章

  1. hdu1213 How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. hdu1213 How Many Tables 并查集的简单应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...

  3. HDU1213 How Many Tables (并查集)

    题目大意: 有一个人要过生日了,请他的朋友来吃饭,但是他的朋友互相认识的才能坐在一起,朋友的编号从1 ~ n,输入的两个数代表着这两个人互相认识(如果1和2认识,2和3认识,那么1和3也就认识了).问 ...

  4. 并查集(Union-Find) 应用举例 --- 基础篇

    本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...

  5. HDU1213:How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. C - How Many Tables - HDU-1213

    某个人举办生日宴会邀请了很多人来参加,不过呢,这些人有个毛病他们只会与熟悉人的坐在一起,当然他们也信奉朋友的朋友也是朋友这一法则,所以问最少需要多少张桌子...... 好吧我承认这才是裸并查集.... ...

  7. HDU1213:How Many Tables(并查集入门)

    -----------刷点水题练习java------------- 题意:给定N点,M边的无向图,问有多少个连通块. 思路:可以搜索;  可以并查集.这里用并查集练习java的数组使用,ans=N, ...

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

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

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

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

随机推荐

  1. Mysql数据库优化总结2

    说明:本文的环境为CENTOS 5.5 64 Bit /Mysql 5.1.50 简介:使用Mysql有一段时间了,期间做了不少关于Mysql优化.设计.维护的工作,这两天有时间做一下简单的总结,方便 ...

  2. 带删除小图标的EditText

    import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawa ...

  3. 5、NFC概述

    什么是NFC NFC(Near Field Communication,近场通信),是一种数据传输技术.但与Wi-Fi.蓝牙.红外线等数据传输技术的一个主要差异就是有效距离一般不能超过4厘米. NFC ...

  4. delphi中通过CreateOleObject操控Word

    http://blog.csdn.net/csm2432/article/details/7692443

  5. oracle 定时任务(DBMS_JOB)

    示例如下: -- 每隔一分钟执行存储过程p1 create or replace procedure p2 as job_num number; begin dbms_job.submit(job_n ...

  6. ID@Xbox计划宣传片 XboxOne喜迎大波小游戏(转)

    微软Xbox One游戏不够多?别担心,微软的ID@Xbox计划将带来一大波独立游戏!微软在今天正式公布了即将登陆旗下的Xbox One主机平台的独立游戏阵容,数量多达32款,官方的宣传片也已放出,感 ...

  7. php获取上传多个文件缺失

    我们的一个页面编辑发布后台出现了图片无法上传保存的情况,经过调试对比发现,原来是file表单数量过多导致,减少file表单的数量即可上传成功.为了满足需求不减少file表单数并保证上传成功,于是更改了 ...

  8. Javascript类型转换表

    各种类型的值 转换为各种类型 String Number Boolean Object undefined "undefined" NaN false 报错 null " ...

  9. 编辑器CocoStudio和CocosBuilder的对比

    来源:http://4137613.blog.51cto.com/4127613/1352805   CocosBuilder CocoStudio 控件种类 支持大部分cocos2d-x自带的常用控 ...

  10. QueryInterface

    QueryInterface IUnknown *p2; hr = pInnerUnknown->QueryInterface(vGUID2, (void**)&p2); IUnknow ...