2647: How Many Tables

时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte
总提交: 353            测试通过:208

描述

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
5 3
1 2
2 3
4 5

5 1
2 5

样例输出

2
4

题目来源

HDOJ

题解:

题目意同上TOJ3136

简单并查集。

#include<stdio.h>
#include<iostream>
using namespace std;
int fid[100000];
int find(int x)
{
if(x==fid[x])
{
return x;
}
else
{
return find(fid[x]);
}
}
int hebing(int x,int y)
{
int x1=find(x);
int y1=find(y);
if(x1!=y1)
{
fid[x1]=y1;
}
}
int main()
{
int n,m,i,j,a,b,s;
int t,l;
scanf("%d",&t);
for(l=0;l<t;l++)
{
s=0;
for(i=0;i<100000;i++)
{
fid[i]=i;
}
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d %d",&a,&b);
hebing(a,b);
}
for(j=1;j<=n;j++)
{
if(fid[j]==j)s++;
}
printf("%d\n",s);
}
}

TOJ2647的更多相关文章

随机推荐

  1. R 语言机器学习同步推进~

    教材就是传说中的机器学习和R语言--中文版,大家可以去图书馆借来看看~~~,例子都是来自书上的 首先介绍一下KNN算法,KNN还好吧,说白了就是一个算距离的公式然后以统计的方式呈现出来,以二维平面为例 ...

  2. win下Redis安装使用

    官网上没有windows版本的Redis,好在github上有大牛写的win版本.地址如下https://github.com/dmajkic/redis/downloads 解压后运行目录下的red ...

  3. win7下安装Sass和compass

    由于项目需要我们使用到sass来编译css文件.本人在win7下开发 由于国内安装sass遇到了一些困难,后来不得不网查询,后来终于解决了,这里介绍一下 1.要安装sass环境必须要先安装rubyIn ...

  4. 用PowerMock mock final类constructors

    也相对简单,直接贴代码 被测方法 public class EmployeeServiceWithParam { public void createEmployee(final Employee e ...

  5. Html5-canvas

    Html5-canvas: 坐标是x向右,逐步增大, y坐标向下增大,原点在画布的左上角.长度单位是一个像素: 像素是一个密度单位,而厘米是长度单位,两者无法比较: Html5的绘图函数: 2.    ...

  6. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. java基础之 超类Object

    一.概述:       Object类是所有Java类的祖先.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法.在不明确给出超类的情况下,Java会自动把Object作为要 ...

  8. iOS架构师之路:慎用继承

    最近在看大神Casa的文章<跳出面向对象思想(一) 继承>,脑洞大开.文章给我们展示了一个随着产品需求不断变化的例子,该例子中通过继承实现不同页面的搜索视图和搜索逻辑的代码复用,随着产品需 ...

  9. 第九章 硬件抽象层:HAL

    这一章介绍HAL,全称为Hardware Abstract Layer,即硬件抽象层,它是建立在Linux驱动之上的一套程序库,程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.为A ...

  10. DTD指定了游戏规则。

    1.DTD的作用 DTD是XML的型,列出了XML中的元素有哪些.元素间的关系.元素可以有哪些内容,元素的属性也有哪些.DTD实质说明的是元素间的关系,也就是类之间的关系.是一棵树状结构的说明,与XM ...