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. strlcpy和strlcat

    strncpy 等主要的问题还是虽然不会溢出,但是满了就不给缓冲区添加0结束符了,以前在项目里面自己还写了个 safe_strcpy 现在发现早就有了 http://blog.csdn.net/lin ...

  2. CSipSimple配置系统

    称作配置系统未免太大了一点,不过它的配置管理这一块确实有加以设计,一方面以增加灵活性,另一方面以支持第三方扩展.通过分析源码,粗略画出如下的结构图: 一.类分析 SharedPreference 一切 ...

  3. FFmpeg:初步编译使用[Android]

    1.安装NDK:http://dl.google.com/android/ndk/android-ndk-r9-linux-x86.tar.bz2 sudo gedit ~/.bashrc 末尾添加: ...

  4. Shell脚本8种字符串截取方法总结

    Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.aaa.com/123.htm. 1. # 号截取,删除左边字符,保留右边字符. 代码如下: echo ${va ...

  5. apache 重定向

    <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTPS} !=on RewriteRule ^(.*) https:// ...

  6. inside the C++ Object model总结

    一. 关于对象 1.内联函数:能够除去函数调用的开支,每一处内联函数的调用都是代码的复制.这是一种空间换取时间的做法,若函数代码量大或者有循环的情况下,不宜内联(这件事有些编译器会自动帮你做).在类中 ...

  7. kendo ui之grid列表

    1.test_grid.jsp <html><head> <%@ include file="/WEB-INF/jsp/common/top.jsp" ...

  8. window下安装pip工具,再利用pip安装工具来安装其他的python包

    1.在安装pip前,请确认你window系统中已经安装好了python,和easy_install工具,如果系统安装成功,easy_install在目录C:\Python27\Scripts 下面,如 ...

  9. mybatis学习2

    解决字段名与实体类属性名不相同的冲突 1. 准备表和数据:CREATE TABLE orders(order_id INT PRIMARY KEY AUTO_INCREMENT,order_no VA ...

  10. 从UWP到SWIFT-开始

    hi,all 我呢,是一个win10 uwp的开发者,从wp7.wp8.wp8.1.win8.1 到现在的win10,一直在windows阵营,做过一些大家比较熟悉的东西现在也还是在做win10的uw ...