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 ...
随机推荐
- 彻底搞懂JavaScript的闭包、防抖跟节流
最近出去面试了一下,收获颇多!!! 以前的我,追求实际,比较追求实用价值,然而最近面试,传说中的面试造火箭,工作拧螺丝,竟然被我遇到了.虽然很多知识点在实际工作中并不经常用到,但人家就是靠这个来筛选人 ...
- Java 8 之前的时间处理
最近在自己瞎折腾,有一个需求是计算当前日期到指定日期有多少天,用于实现纪念日或倒计时的功能.查阅资料后发现Java 8之前的时间日期处理很是麻烦而且被频频吐槽,以至于后来在 Java 8 中推出了一个 ...
- oracle range分区表已经有了MAXVALUE 分区,如何添加分区?要不能删除MAXVALUE分区里的数据,不影响在线应用。
来做个实验说明该问题:1.创建个分区表SQL> create table p_range_test 2 (id number,name varchar2(100)) 3 partition by ...
- 如何使用 VS Code开发.NET Core应用程序
Visual Studio Code(VS Code)是Microsoft为Windows,Linux和Mac操作系统开发的免费,跨平台,轻量级的源代码编辑器,它是源代码编辑器,而Visual Stu ...
- CopyOnWriteArrayList设计思路与源码分析
CopyOnWriteArrayList实现了List接口,RandomAccess,Cloneable,Serializable接口. CopyOnWriteArrayList特性 1.线程安全,在 ...
- zabbix 4.X 版本 web字体显示方块
先看看问题长啥样...... Zabbix 字体乱码(显示呈现方块) 第一种解决方法: # 1) 进入代码存放目录的字体目录: cd /data/www/zabbix/assets/fonts # 2 ...
- Linux学习笔记 | 常见错误之账户密码正确但是登录不进去系统
前言: 笔者今日由于Linux版本的原因,需要Linux内核版本不能太高的系统,而日常使用的ubuntu系统不能满足需求,于是新建了一个虚拟机,选用的系统是Ubuntu16的,配置了一下午的各种依赖环 ...
- 5.1中repair table
mysql> repair table xs;+---------+--------+----------+----------+| Table | Op | Msg_type | Msg_te ...
- 【Oracle】查看哪些用户被授予了DBA权限
查看哪些用户被授予了DBA权限 select * from dba_role_privs where granted_role='DBA'; 回收权限: revoke dba from xxx;
- Python设计模式面向对象编程
前言 本篇文章是基于极客时间王争的<设计模式之美>做的总结和自己的理解. 说到面向对象编程,作为一个合格的Pythoner,可以说信手拈来.毕竟在Python里"万物都是对 ...