题目链接: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. python 中dir()和__dict__的区别

    Python __dict__与dir() 出处(http://blog.csdn.net/lis_12/article/details/53521554). Python下一切皆对象,每个对象都有多 ...

  2. 关于error:Cannot assign to 'self' outside of a method in the init family

    有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Can ...

  3. Android输出日志到电脑磁盘

    使用Eclipse查看Log有时候挺恶心的,有些Log ADB会自动的清除,所有有时候导致抓不到有效的Log,把Log保存到文件,然后通过文本查看器查看,感觉好Happy,下面就是脚本文件: adb ...

  4. Faulty Odometer(九进制数)

    Faulty Odometer Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9301   Accepted: 5759 D ...

  5. Java并发编程、内存模型与Volatile

    http://www.importnew.com/24082.html  volatile关键字 http://www.importnew.com/16142.html  ConcurrentHash ...

  6. MySQL开发面试题

    ……继上一篇MySQL的开发总结之后,适当的练习还是很有必要的…… SQL语法多变,不敢保证唯一,也不敢保证全对,如果错误欢迎指出,即刻修改. 一.现有表结构如下图 TABLENAME:afinfo ...

  7. 【Leaflet】鼠标提取坐标

    map.on('mousemove', function (e) { document.getElementById('info').innerHTML = /* innerHTML 属性设置或返回表 ...

  8. Splash go() 方法

    go()方法用来请求某个链接,而且它可以模拟 GET 和 POST 请求,同时支持传入请求头.表单等数据 function main(splash) ok, reason = splash:go(&q ...

  9. drawCall_01

    在屏幕上渲染物体,引擎需要发出一个绘制调用来访问图形API(iOS系统中为OpenGL ES).每个绘制调用需要进行大量的工作来访问图形API,从而导致了CPU方面显著的性能开销.   Unity在运 ...

  10. (数字IC)低功耗设计入门(一)——低功耗设计目的与功耗的类型

    低功耗设计这个专题整理了好久,有一个月了,有图有证据: 然而最近一直有些烦心事.郁闷事,拖延了一下,虽然现在还是有点烦,但是还是先发表了吧.下面我们就来聊聊低功耗设计吧,由于文章比较长,因此我就不一次 ...