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.

InputThe 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. 
OutputFor 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 这个题做麻烦了,其实可以直接遍历pre数组,其中pre[i]==i的个数就是所有集合的个数!
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 30000
#define INF 0x3f3f3f3f
/*
并查集
*/
set<int> s;
int pre[MAXN],n,m;
int find(int x)
{
if(x==pre[x])
return x;
else
return pre[x] = find(pre[x]);
}
void mix(int x,int y)
{
int fx = find(x),fy = find(y);
if(fx!=fy)
pre[fy] = fx;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int x,y;
cin>>n>>m;
for(int i=;i<=n;i++)
pre[i] = i;
s.clear();
for(int i=;i<m;i++)
{
cin>>x>>y;
mix(x,y);
}
int cnt = ;
for(int i=;i<=n;i++)
{
int fi = find(i);
if(!s.count(fi))
{
cnt++;
s.insert(fi);
}
}
cout<<cnt<<endl;
}
return ;
}

C - How Many Tables 并查集的更多相关文章

  1. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  2. hdu1213 How Many Tables(并查集)

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

  3. POJ-1213 How Many Tables( 并查集 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...

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

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

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

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

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

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

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

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

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

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

  9. 并查集-F - How Many Tables

    F - How Many Tables 并查集的模板都能直接套,太简单不注释了,就存个代码 #include<bits/stdc++.h> using namespace std; ; i ...

随机推荐

  1. [Qt Creator 快速入门] 第4章 布局管理

    第3章讲述了一些窗口部件,当时往界面上拖放部件时都是随意放置的,这对于学习部件的使用没有太大的影响,但是,对于一个完善的软件,布局管理却是必不可少的. 无论是想要界面中部件有一个很整齐的排列,还是想要 ...

  2. Linux下磁盘分区、挂载、卸载操作记录

    Linux下磁盘分区.挂载.卸载操作记录. 操作环境:CentOS release 6.5 (Final) Last :: from 118.230.194.76 [root@CentOS ~]# [ ...

  3. 在C语言中模仿java的LinkedList集合的使用(不要错过哦)

    在C语言中,多个数据的储存通常会用到数组.但是C语言的数组有个缺陷,就是固定长度,超过数组的最大长度就会溢出.怎样实现N个数储存起来而不被溢出呢. 学过java的都知道,java.util包里有一个L ...

  4. <mybatis:scan>与<MapperScannerConfigurer/>

    使用Mybatis作为持久层的框架,对dao层的bean对象的注解扫描有两种方式:<mybatis:san>.<MapperScannerConfigurer> 一:<m ...

  5. Verification Mind Games---how to think like a verifier像验证工程师一样思考

    1. 有效的验证需要验证工程师使用不同于设计者的思维方式思考问题.具体来说,验证更加关心在严格遵循协议的基础上发现设计里面的bug,搜索corner cases,对设计的不一致要保持零容忍的态度. m ...

  6. MaskRCNN:三大基础结构DeepMask、SharpMask、MultiPathNet

    MaskXRCnn俨然成为一个现阶段最成功的图像检测分割网络,关于MaskXRCnn的介绍,需要从MaskRCNN看起. 当然一个煽情的介绍可见:何恺明团队推出Mask^X R-CNN,将实例分割扩展 ...

  7. Redis系列(六)--为什么这么快?

    Redis作为一个基于key-value的NoSQL数据库,最显著的特点存取速度非常快,官方说可以达到10W OPS,但是Redis为何这么快? 1.开发语言 Redis使用C语言进行编写的,而Uni ...

  8. Extjs杂记录

    1,页面跳转到另外一个页面 这段话的意思:取得恢复密码窗口,关闭这个窗口,页面跳转到Login页面 2,keypecial 当与导航相关的键(如箭头.tab键.Enter键.ESC键等)按下时,该事件 ...

  9. Semi-colon expected (eclipse 引入 json文件报错)

    最近做的项目用到的前端框架有一个json文件夹,里面全是json文件,所以导入Eclipse的时候会在整个项目上都是红叉,但是其实不影响项目运行的,之前忙着码代码也没时间管他,这个红叉存留了一个月!今 ...

  10. Servlet的说明及使用案例

    Servlet的说明及使用案例 制作人:全心全意 Servle的基础介绍 Servlet结构体系 Servlet对象.ServletConfig对象与Serializable对象是接口对象,其中Ser ...