题目大致意思:

有n个人在一起吃饭,有些人互相认识。认识的人想坐在一起,不想跟陌生人坐。例如A认识B,B认识C,那么A、B、C会坐在一张桌子上。

给出认识的人,问需要多少张桌子

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. 

InputThe 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. 
OutputFor each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks. 
Sample Input

2
5 3
1 2
2 3
4 5 5 1
2 5

Sample Output

2
4

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int mp[1010][1010];
int pre[1010];
int n,m;
void init()//初始化先一人一张桌子
{
for(int i=1;i<=n;i++)
{
pre[i]=i;
}
}
int find(int x)//查找认识的人
{
return x==pre[x]?x:find(pre[x]);
}
void merge(int x,int y)//将他们合并为一桌
{
int tx=find(x);
int ty=find(y);
if(tx!=ty)
{
pre[tx]=ty;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(mp,0,sizeof(mp));
cin>>n>>m;
memset(pre,0,sizeof(pre));
init();
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
merge(x,y);
}
int ans=0;
for(int i=1;i<=n;i++)//统计有多少个集合
{
if(pre[i]==i)
ans++;
}
cout<<ans<<endl;
}
}

B - How Many Tables (多少桌)的更多相关文章

  1. hdu 1213 How Many Tables(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...

  2. Codeforces Round #423 A Restaurant Tables(模拟)

    A. Restaurant Tables time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. 题解报告:hdu 1213 How Many Tables

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

  4. 并查集---体会以及模板&&How Many Tables - HDU 1213

    定义&&概念: 啥是并查集,就是将所有有相关性的元素放在一个集合里面,整体形成一个树型结构,它支持合并操作,但却不支持删除操作 实现步骤:(1)初始化,将所有节点的父亲节点都设置为自己 ...

  5. LOCK TABLES和UNLOCK TABLES与Transactions的交互

    LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...

  6. 函数的使用顺序---TABLES,USING,CHANGING

    SAP使用PERFORM的时候: ... [TABLES   itab1 itab2 ...]     [USING    a1 a2 ...]     [CHANGING a1 a2 ...]. E ...

  7. Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement

    Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...

  8. mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts' when using LOCK TABLES

    AutoMySQLBackup备份时,出现mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@' ...

  9. Neutron 理解 (4): Neutron OVS OpenFlow 流表 和 L2 Population [Netruon OVS OpenFlow tables + L2 Population]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

随机推荐

  1. node中 path.join 和 path.resovle 区别

    console.log(__dirname) console.log('----') console.log(path.resolve(__dirname, '/a/b', '../')) conso ...

  2. C++ 中的 inline 详解

    inline:是一个关键词,放在一个函数前面,说明这个函数是inline函数. inline函数是什么?inline有什么作用? 为了解答这个问题,我们首先要知道编译器是如何为我们工作的. 先看一段代 ...

  3. Linux Clone函数

    Linux Clone函数 之前某一次有过一次面试,问了内核中是怎么创建命名空间的? 下面就来扒一扒clone的精髓,以及如何通过它创建命名空间. 目录 Linux Clone函数 使用clone创建 ...

  4. 【Java】运算符(算术、赋值、比较(关系)、逻辑、条件、位运算符)

    运算符 文章目录 运算符 1. 算术运算符 2. 赋值运算符 3. 比较运算符 4. 逻辑运算符 5. 条件运算符 6. 位运算符 7. 运算符优先级 8. 运算符操作数类型说明 9.code 算术运 ...

  5. UNDO表空间切换步骤

    1.新建UNDO表空间 create undo tablespace UNDOTBS2 datafile '/data01/testdb/undotbs01.dbf' size 1G; alter d ...

  6. postgresql中权限介绍

    postgresql权限分为实例的权限,数据库的权限,模式的权限,对象的权限,表空间的权限 实例的权限:由pg_hba.conf文件控制,控制那些用户那些IP以哪种方式连接数据库 数据库的权限:是否允 ...

  7. dblink查找对应的目标端session

    v$session试图中process字段代表的是客户端所在机器的进程号 例如我使用toad连接数据库,查询到的process即toad的进程号 SELECT process FROM V$SESSI ...

  8. Gulp4.0入门和实战

    gulp4.0入门和实战 我最近遇到需要优化web的性能的任务,然后就捣鼓了一些对资源文件优化压缩的方案.由于之前的项目中有使用到gulp,所以在需要处理的web项目中也优先使用这个技术. 先聊聊gu ...

  9. Service Locator Pattern 服务定位

    https://www.geeksforgeeks.org/service-locator-pattern/ Service Locator Pattern Last Updated: 06-03-2 ...

  10. 有状态 无状态 stateful stateless monolithic architecture microservice architecture 单体架构

    为什么游戏公司的server不愿意微服务化? - 知乎 https://www.zhihu.com/question/359630395 我大概说了,方便测试,方便维护,方便升级,服务之间松耦合,可多 ...