题目描述

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.

输入

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.

输出

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

样例输入

2 6 4 1 2 2 3 3 4 1 4 8 10 1 2 2 3 5 6 7 5 4 6 3 6 6 7 2 5 2 4 4 3

样例输出

3 2
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; int f[]; void init()
{
for(int i=;i<;i++) f[i]=i;
} int findFather(int x)
{
int a=x;
while(x!=f[x])
{
x=f[x];
}
while(a!=f[a])
{
int z=a;
a=f[a];
f[z]=x;
}
return x;
} int cnt=; void unionf(int a,int b)
{
int fa=findFather(a);
int fb=findFather(b);
if(fa!=fb)
{
cnt-=;
f[fa]=fb;
}
} int main()
{
int t;
cin>>t;
for(int i=;i<t;i++)
{
init();
int n,m;
cin>>n>>m;
cnt=n;
for(int j=;j<m;j++)
{
int a,b;
cin>>a>>b;
unionf(a,b);
}
cout<<cnt<<endl;
}
}

[并查集] How Many Tables的更多相关文章

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

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

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

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

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

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

  4. hdu 1213 (How Many Tables)(简单的并查集,纯模板)

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

  5. HDU1213:How Many Tables(并查集)

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

  6. 并查集:HDU1213-How Many Tables(并查集最简单的应用)

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

  7. HDU——1213How Many Tables(并查集按秩合并)

    J - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  8. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

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

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

随机推荐

  1. 基于STM32的简易磁卡充值系统

    使用的是MFRC522射频模块,把磁卡放入感应区后,可以执行三种操作: 初始化磁卡金额 读取卡内金额 向卡内写入金额(充值) 本来想着回学校了能把洗浴卡的金额给改掉,实现帝皇般的尊贵洗浴享受(不花钱… ...

  2. 网站漏洞修复案例之Discuz!3.4最新版本

    Discuz!论坛目前最新版本为3.4版本,已经好久没有更新了,我们SINE安全在对其网站安全检测的同时发现一处漏洞,该漏洞可导致论坛的后台文件可以任意的删除,导致网站瘫痪,后台无法登陆.关于该网站漏 ...

  3. 在Ubuntu上开启MongoDB的IP Security

    本文翻译之MongoDB官网博客,原地址:https://www.mongodb.com/blog/post/enabling-ip-security-for-mongodb-36-on-ubuntu ...

  4. Springboot启动报Multiple Dockets with the same group name are not supported. The following duplicate groups were discovered.

    解决方法: 属于bean重复,根据错误提示剔除多于的Bean引用!

  5. WPF实现拖拽功能

    技术点:WPF的Behaviors实现了对象的行为附加,Microsoft.Expression.Interactions程序集中包含了若干Behaviors,其中MouseDragElementBe ...

  6. apt-get 0%

    错误如下:      www.2cto.com   使用apt-get命令安装软件时出现“0%[正在等待报头]“的提示且无法继续安装,一般出现此问题是因为安装过程中Ctrl+C强行中断或其它原因导致上 ...

  7. 【转载】OGRE 2.1 Windows 编译

    OGRE 2.1 Windows 编译 环境: Windows 7 64Bit Visual Studio 2012 OGRE 2.1 CMake 2.8.12.1 OGRE: OGRE官方推出了最新 ...

  8. 1245 最小的N个和

    1245 最小的N个和 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond         题目描述 Description 有两个长度为 N 的序列 A 和 B, ...

  9. kali安装后相关软件的配置

    更新软件apt-get updateapt-get upgrade安装输入法apt-get install ibus-pinyin apt-get install netspeed安装GNOMEapt ...

  10. Azkaban 工作流调度器

    Azkaban 工作流调度器 1 概述 1.1 为什么需要工作流调度系统 a)一个完整的数据分析系统通常都是由大量任务单元组成,shell脚本程序,java程序,mapreduce程序.hive脚本等 ...