传送门

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

思路

需要的最少桌子数即为联通块的个数。

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1005;
int fa[maxn],flag[maxn];

int find(int x)
{
    int r = x;
    while (r != fa[r])    r = fa[r];
    int i = x,j;
    while (i != r)
    {
        j = fa[i];
        fa[i] = r;
        i = j;
    }
    return r;
} 

void unite(int x,int y)
{
    x = find(x),y = find(y);
    if (x != y)    fa[x] = y;
}

int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        int N,M,x,y,res = 0;
        memset(flag,false,sizeof(flag));
        scanf("%d%d",&N,&M);
        for (int i = 1;i <= N;i++)    fa[i] = i;
        while (M--)
        {
            scanf("%d%d",&x,&y);
            unite(x,y);
        }
        for (int i = 1;i <= N;i++)    if (find(i) == i)    res++;
        printf("%d\n",res);
    }
    return 0;
}

  

HDU 1213 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. 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. hdu 1213 求连通分量(并查集模板题)

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

  7. HDU 1213 How Many Tables(模板——并查集)

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

  8. HDU 1213 How Many Tables(并查集模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...

  9. HDU - 1213 How Many Tables 【并查集】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...

  10. HDU 1213 How Many Tables (并查集)

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

随机推荐

  1. Spring MVC 急速集成 Shiro 实录

    相信有很多的程序员,不愿意进行用户管理这块代码实现. 原因之一,不同的JavaEE 系统,用户管理都会有个性化的实现,逻辑很繁琐. 而且是系统门面,以后背锅的几率非常大,可谓是低收益高风险. 最近在系 ...

  2. 用js转换joson返回数据库的时间格式为/Date(*************)/

    原理是取中间的毫秒数,再转换成js的Date类型 function ChangeDateFormat(val) { if (val != null) { var date = new Date(par ...

  3. SDRAM操作原理分析

    芯片原理图 引脚原理图 指令 通过对上面指令的总结,简化出要用到的指令如下: 指令 常量名 CKE CSn RAS CASn WEn 备注 空操作 NOP 1 0 1 1 1   行激活 ACTIVE ...

  4. matlab中的卷积——filter,conv之间的区别

    %Matlab提供了计算线性卷积和两个多项式相乘的函数conv,语法格式w=conv(u,v),其中u和v分别是有限长度序列向量,w是u和v的卷积结果序列向量. %如果向量u和v的长度分别为N和M,则 ...

  5. 记一次在Eclipse中用Axis生成webservice服务端的过程中出现的问题

    问题一. Unable to find config file.  Creating new servlet engine config file: /WEB-INF/server-config.ws ...

  6. jQuery基础--样式篇(2)

    老套路,最开始上手就是写一个“Hello world!”,使用jQuery后获取元素十分简单,如下面获取div的对象,原生的javascript代码:document.getElementsByTag ...

  7. IntelliJ_编译一直报错“找不到符号”

    执行maven compile时一直报错"找不到符号",类 XXX 各种clean.compile都不行 最后执行Rebuild Project一次后解决   执行rebuild ...

  8. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  9. OkHttp:Java 平台上的新一代 HTTP 客户端

    OkHttp 简介 OkHttp 库的设计和实现的首要目标是高效.这也是选择 OkHttp 的重要理由之一.OkHttp 提供了对最新的 HTTP 协议版本 HTTP/2 和 SPDY 的支持,这使得 ...

  10. 51nod 1040最大公约数和(欧拉函数)

    1040 最大公约数之和 题目来源: rihkddd 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 给出一个n,求1-n这n个数,同n的最大公约数 ...