传送门

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. SQL Server Management Studio无法记住密码

    用sa账户登录sql server 2008,勾选了“记住密码”,但重新登录时,SQL Server Management Studio无法记住密码.   后来发现,在重新登录时,登录名显示的并非是s ...

  2. WampServer下如何实现多域名配置(虚拟域名配置)

    之前在学习跨域的时候,我写过一篇叫做WampServer下使用多端口访问的文章,默认的 localhost 采用的是 80 端口,能使用多端口访问的核心是得新建一个端口,也就是新建一个 http 服务 ...

  3. jQuery学习笔记(四):attr()与prop()的区别

    这一节针对attr()与prop()之间的区别进行学习. 先看看官方文档是如何解释两者之间功能差异的: attr() Get the value of an attribute for the fir ...

  4. ASP.NET + SqlSever 大数据解决方案 PK HADOOP

    半个月前看到博客园有人说.NET不行那篇文章,我只想说你们有时间去抱怨不如多写些实在的东西.  1.SQLSERVER优点和缺点? 优点:支持索引.事务.安全性以及容错性高 缺点:数据量达到100万以 ...

  5. flex弹性布局学习笔记

    前言 资料来源于网络,本人只是对此作了一下操作,记录于此以便以后查阅.目的在于梳理自己前端凌乱的知识点. 本文根据 Brian Franco 的一个flexbox.scss库来记录 入职新公司前对移动 ...

  6. Java关键字this、super使用总结

    版权声明:原创作品,如需转载,请与作者联系.否则将追究法律责任. 作者:熔岩日期:2007-03-01MSN :leizhimin@126.com声明:原创作品,未经授权,谢绝转载! 好久没有对所学知 ...

  7. Eclipse利用Maven2搭建SpringMVC框架的Web工程

    一.准备工作: 下载apache-maven--> 配置Maven_home -->下载Eclipse Maven插件 二.新建工程:   选择新建Maven Project  arche ...

  8. C 语言学习的第 04 课:编译器常见错误和警告(1)

    同学们可能已经开始使用 C-Free 5 写自己的程序了.但是新手编程,总是会有一些磕磕绊绊.不要紧,在这篇文章中,就主要来了解一些编程开始时经常会遇到的语法方面的问题. warning: no ne ...

  9. Vue-loader 开启压缩后的一些坑

    在使用vue-loader 配合webpack 对.vue文件进行加载的时候,如果开启了代码压缩会出来下面几种问题,做个记录. 丢失td结束标记,导致页面的布局错乱 input的属性type为text ...

  10. 【Alpha版本】冲刺阶段——Day 9

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...