题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213

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

并查集的模板题。

#include<bits/stdc++.h>
using namespace std;
const int maxn=+; int n,m; int par[maxn],ran[maxn];
void init(int l,int r){for(int i=l;i<=r;i++) par[i]=i,ran[i]=;}
int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));}
void unite(int x,int y)
{
x=find(x), y=find(y);
if(x==y) return;
if(ran[x]<ran[y]) par[x]=y;
else par[y]=x, ran[x]+=(ran[x]==ran[y]);
}
bool isSame(int x,int y){return find(x)==find(y);} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m); init(,n);
for(int i=,a,b;i<=m;i++)
{
scanf("%d%d",&a,&b);
if(!isSame(a,b)) unite(a,b);
} set<int> S;
for(int i=;i<=n;i++) S.insert(find(i)); printf("%d\n",S.size());
}
}

整理一下并查集的两种模板吧:

int par[maxn],ran[maxn];
void init(int l,int r){for(int i=l;i<=r;i++) par[i]=i,ran[i]=;}
int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));}
void unite(int x,int y)
{
x=find(x), y=find(y);
if(x==y) return;
if(ran[x]<ran[y]) par[x]=y;
else par[y]=x, ran[x]+=(ran[x]==ran[y]);
}
bool isSame(int x,int y){return find(x)==find(y);}
int par[maxn];
void init(int l,int r){for(int i=l;i<=r;i++) par[i]=i;}
int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));} //这种简单的并查集的合并方式是:
int t1=find(u),t2=find(v);
if(t1!=t2) par[t1]=t2; //这种是把点u所在树并入点v所在树
if(t1!=t2) par[t2]=t1; //这种是把点v所在树并入点u所在树

HDU 1213 - How Many Tables - [并查集模板题]的更多相关文章

  1. hdu 1213 求连通分量(并查集模板题)

    求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

  2. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  3. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

  4. HDU 1213 How Many Tables (并查集,常规)

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  5. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  6. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  7. 杭电ACM省赛集训队选拔赛之热身赛-How Many Tables,并查集模板题~~

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

  8. PAT甲题题解-1114. Family Property (25)-(并查集模板题)

    题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房 ...

  9. HDU-1232/NYOJ-608畅通工程,并查集模板题,,水过~~~

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) http://acm. ...

随机推荐

  1. Dubbo -- 系统学习 笔记 -- 示例 -- 负载均衡

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 负载均衡 在集群负载均衡时,Dubbo提供了多种均衡策略,缺省为random随机调 ...

  2. 【Python】Excel处理

    1.包导入 from openpyxl import Workbook from openpyxl import load_workbook from openpyxl.compat import r ...

  3. GreenPlum数据库安装

    第一章    文档概述 本文描述适用于Greenplum4.0以上版本的安装操作.所涉及到的操作系统相关参数调整,主要针对Redhat Linux操作系统. 第二章    安装介质 操作系统:Cent ...

  4. Redis存读取数据

    这一节演示下载.NET中怎样使用Redis存储数据.在.net中比较常用的客户端类库是ServiceStack,看下通过servicestack怎样存储数据. DLL下载:https://github ...

  5. Linux man 命令

    man命令可以用来查看Linux命令的帮助信息 .配置文件的帮助信息等等,通过不同的代号可以查看不同的帮助信息: 代号 含义 1 查看Linux命令的帮助信息(默认) 2 查看内核提供的函数的帮助信息 ...

  6. mybayis 之resultType="map"

    List<Map> publishInfos = memberShareMapper.shareToCouponCountGroupByPublishId(memberShare.getA ...

  7. thinkjs 中增加过期时间

    使用thinkjs搭建的项目需要实现一小时后过期的功能:于是对比了新建项目与原有项目的不同之处:         官网中给的介绍:https://thinkjs.org/zh-cn/doc/2.2/a ...

  8. 更新npm至最新版本

    npm install npm@latest –g 或者@ 符号后面直接添加你想更新到的版本号

  9. MVC的初步认识理论

    说起来写博客可以说一个月没来啦,我们狠狠的放假一个月,想一想都奇怪.而是想一下以后的假期还会这样吗?或许这是作为学生的我们的最后一个长的假期啦,以后就要面对工作再也没有寒假暑假之分啦,在这一个月的时间 ...

  10. PHP 搜索分词实现代码

    <?php /** * @author: xiaojiang 2014-01-08 * php 建立分词树 * */ class Tree{ public $w = ''; public $su ...