B - How Many Tables (多少桌)
题目大致意思:
有n个人在一起吃饭,有些人互相认识。认识的人想坐在一起,不想跟陌生人坐。例如A认识B,B认识C,那么A、B、C会坐在一张桌子上。
给出认识的人,问需要多少张桌子
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 (多少桌)的更多相关文章
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- Codeforces Round #423 A Restaurant Tables(模拟)
A. Restaurant Tables time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 题解报告:hdu 1213 How Many Tables
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...
- 并查集---体会以及模板&&How Many Tables - HDU 1213
定义&&概念: 啥是并查集,就是将所有有相关性的元素放在一个集合里面,整体形成一个树型结构,它支持合并操作,但却不支持删除操作 实现步骤:(1)初始化,将所有节点的父亲节点都设置为自己 ...
- LOCK TABLES和UNLOCK TABLES与Transactions的交互
LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...
- 函数的使用顺序---TABLES,USING,CHANGING
SAP使用PERFORM的时候: ... [TABLES itab1 itab2 ...] [USING a1 a2 ...] [CHANGING a1 a2 ...]. E ...
- 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 ...
- 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'@' ...
- Neutron 理解 (4): Neutron OVS OpenFlow 流表 和 L2 Population [Netruon OVS OpenFlow tables + L2 Population]
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
随机推荐
- 【JDBC核心】实现 CRUD 操作
实现 CRUD 操作 操作和访问数据库 数据库连接被用于向数据库服务器发送命令和 SQL 语句,并接受数据库服务器返回的结果.其实一个数据库连接就是一个 Socket 连接. java.sql 包中有 ...
- MySQL45讲笔记-事务隔离级别,为什么你改了数据我看不见
简单来说,事务就是要保证一组数据库操作,要么全部成功,要么全部失败.在MySQL中,事务至此是在引擎层实现的,但并不是所有的MySQL引擎都支持事务,这也是MyISAM被InnoDB取代的原因之一. ...
- 在CentOS上安装Singularity高性能容器
什么是singularity容器 Singularity是劳伦斯伯克利国家实验室专门为大规模.跨节点HPC和DL工作负载而开发的容器化技术.具备轻量级.快速部署.方便迁移等诸多优势,且支持从Docke ...
- 【Redis】Redis基础 - Redis安装启动测试
Redis基本 - 安装 文章目录 Redis基本 - 安装 Linux下安装Redis Docker 方式 Github 源码编译方式 直接安装方式 Windows下Redis安装 记录 - Red ...
- Java安全之Weblogic 2018-3248分析
Java安全之Weblogic 2018-3248分析 0x00 前言 基于前面的分析,后面的还是主要看补丁的绕过方式,这里就来简单的记录一下. 0x01 补丁分析 先来看看补丁细节 private ...
- Sentry(v20.12.1) K8S 云原生架构探索,JavaScript 性能监控之管理 Transactions
系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...
- hive窗口函数/分析函数详细剖析
hive窗口函数/分析函数 在sql中有一类函数叫做聚合函数,例如sum().avg().max()等等,这类函数可以将多行数据按照规则聚集为一行,一般来讲聚集后的行数是要少于聚集前的行数的.但是有时 ...
- SourceGenerator入门指北
SourceGenerator介绍 SourceGenerator于2020年4月29日在微软的.net blog首次介绍,大概说的是开发者编可以写分析器,在项目代码编译时,分析器分析项目既有的静态代 ...
- 微信登录4-开发回调URL
一.准备 1.引入pom依赖 在要使用HttpClient的项目中加入依赖 <!--httpclient--> <dependency> <groupId>org. ...
- 最佳的思维导图生成工具——markmap 使用教程
前言 相信很多程序员朋友都有在用 Markdown 吧,我是大三找实习工作的时候接触到的,简历就是用 Markdown 写的. Markdown 的好处是专注码字的同时还能兼顾排版,不用像 word ...