TOJ2647
2647: How Many Tables
总提交: 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
题目来源
题解:
题目意同上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的更多相关文章
随机推荐
- iOS安全—阻止tweak注入hook api
http://blog.csdn.net/zcrong/article/details/51617348 在Other Linker Flags中添加: -Wl,-sectcreate,__RESTR ...
- Android中Activity加入Fragment使用注意事项及常用技巧
Fragment中AlertDialog弹出窗口的使用 Fragment默认不具有Content的一些方法和属性,因此在Activity中可以使用的一些方法在Fragment中使用就需要一些小技巧了 ...
- 两台装有Ubuntu系统的服务器搭建VPN(一台为本地服务器,另一台为云服务器)
我们搭建VPN采用的是openvpn,搭建过程总体需要经过三大步骤: 1.openvpn的安装与配置 2.端口转发 3.系统重启iptables规则自动生效 注意:以下所有名令在ro ...
- 关于lambda表达式的一些学习——基于谓词筛选值序列
今天看了一些关于lambda表达式的知识,然后对于Func<T,TResult>泛型委托不太熟悉,便查了查相关资料,又引出来了基于谓词筛选值序列这个对我来说的新鲜知识点,于是去查MSDN, ...
- windows7 64位系统pl/sql 客户端的安装
解压将下载到的将其解压,如我解压到了 E:\app\instantclient_11_2 3.设置PLSQL Developer在tools-prefrences,conncetion,OCI lib ...
- C/C++技术常用网站
软件下载网站[visual studio 2005编译器] http://www.xdowns.com/ debug调试大牛 http://blogs.msdn.com/oldnewthing/ ht ...
- java基础之 序列化
一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬 ...
- INSERT INTO 语句的语法错误【 OLE报错,office终端执行SQL没有问题】
表名,字段在代码执行之前一定要进行" [ 字段.表名 ] "中括号包裹,不然会报INSERT INTO 语句的语法错误! office终端没有报错的原因,应该是office在执行之 ...
- Delphi用QJSON解析JSON格式的数据
本来用superobject来解析JSON已经够用了,可惜这个东东不能在移动端使用,于是找到QJSON来处理. 这是一个国内高手写开源免费的东西,赞一个. 假入数据如下: {"message ...
- C++程序内存泄漏检测方法
一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...