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. C#机器视觉入门系列1-转化为灰度图&&3*3模糊

    这是我入门机器视觉的系列学习经验之开篇,本来想着依靠opencv快速实现一些功能,但是想了一下既然是学数学的,还是应该自己多算算,写一些自己理解的东西才好. 入门篇很简单,就只是实现了转化成灰度图以及 ...

  2. 入门struts2.0

    框架是什么? 1.应用程序的半成品. 2.可重用行公共的结构. 3.按一定规则组织的一组组件. model2 其实并不是一种全新的概念,很对人指出model2其实正好是经典的"模型(mode ...

  3. 黑马程序员——C语言基础 枚举 宏定义 自定义 static exterm

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)枚举 1)枚举类型的定义 枚举是C语言中的一种基本数据类型,并不是构 ...

  4. AX多线程编译

    1.在命令行里先定位到AOS sever的BIN文件夹下(CD "AOS sever的BIN路径") CD C:\Program Files\Microsoft Dynamics ...

  5. Scala深入浅出实战经典-----002Scala函数定义、流程控制、异常处理入门实战

    002-Scala函数定义.流程控制.异常处理入门实战 Scala函数定义 语句结束无分号 定义无参函数 def 函数名称(参数名称:参数类型)[:Unit=]{ 函数体 } 老师的代码 我的实际代码 ...

  6. Apache Flume 1.7.0 发布,日志服务器

    Apache Flume 1.7.0 发布了,Flume 是一个分布式.可靠和高可用的服务,用于收集.聚合以及移动大量日志数据,使用一个简单灵活的架构,就流数据模型.这是一个可靠.容错的服务. 本次更 ...

  7. OpenCV在VS2005下的配置

    http://bbs.ednchina.com/BLOG_ARTICLE_3019518.HTM 这是自己今天刚弄好了之后的一点经历.不是很详细.以后有时间一定要整理整理.

  8. css 笔记

    外边距合并 当一个元素出现在另一个元素的上面时,第一个元素的下外边距和第二个元素的上外边距会产生合并,两个盒子之间的上下间距为大的数值. 当一个子元素包含在另外一个父元素(假设没有内边距 没有边框), ...

  9. Bootstrap 3 模态框播放视频

    Bootstrap 3 模态框播放视频 I'm trying to use the Modal feature from Bootstrap 3 to show my Youtube video. I ...

  10. node_modules\typescript\lib 未指向有效的 tsserver 安装 将禁用TypeScript 语言功能

    Ionic2 项目中经常遇到这个问题 每次都找半天无果. 简单记录一下  粗暴的解决办法: 卸载ts并从新安装即可 //卸载typescript npm uninstall typescript // ...