How Many Tables

news/2025/3/19 11:11:36/

一、题目

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.
Input
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.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample
Inputcopy Outputcopy
2
5 3
1 2
2 3
4 5

5 1
2 5
2
4

二、分析

简单的并查集题,求最少需要多少桌子,即最少有几个团体
使用set去重,那么set的size就是答案了。

#include<iostream>
#include<cstring>
#include<set>
using namespace std;
int fa[1100];
int find(int x)
{if(x==fa[x]) return x;fa[x]=find(fa[x]);return fa[x];
}
void merge(int x,int y)
{fa[find(x)]=find(y);
}
int main()
{int T;cin>>T;while(T--){memset(fa,0,sizeof(fa));int n,m;cin>>n>>m;for(int i=1;i<=n;i++){fa[i]=i;}for(int i=1;i<=m;i++){int a,b;cin>>a>>b;merge(a,b);}int f=-999;int ans=0;set<int>qqq;//用于去重for(int i=1;i<=n;i++){qqq.insert(find(i));}cout<<qqq.size()<<endl;}
}

http://www.ppmy.cn/news/1016489.html

相关文章

__dict__属性

__dict__ 是 Python 中的一个特殊属性&#xff0c;通常存在于大多数 Python 对象中&#xff0c;用于存储该对象的可变属性。 以下是关于 __dict__ 的一些关键点和详细信息&#xff1a; 存储属性&#xff1a;对于大多数自定义的 Python 对象&#xff0c;__dict__ 属性包含了这个…

安全杂记 - Linux文本三剑客之awk

目录 1.什么是AWK2.正则表达式3.语法4.内置变量示例printf命令5.复现awk经典实例(1).插入几个新字段(2).格式化空白(3).筛选IPv4地址(4).筛选给定时间范围内的日志 1.什么是AWK awk、grep、sed是linux操作文本的三大利器&#xff0c;合称文本三剑客。三者的功能都是处理文本&a…

【JavaScript 】浏览器事件处理

1. 什么是浏览器事件? 浏览器事件是指在网页中发生的各种交互和动作,例如用户点击按钮、页面加载完成、输入框文本变化等。通过处理这些事件,可以编写相应的JavaScript代码来实现特定的功能和行为。 2. 常见的浏览器事件 以下是一些常见的浏览器事件及其用途的详细介绍: c…

[服务被植入脚本,数据泄密?数据库如何做内网访问?]

目录 前言: 数据库内网访问的方式: 使用nginx进行反向代理可以实现将外部用户的请求转发到内网中的数据库服务器上。具体步骤如下&#xff1a; 使用SSH隧道可以建立加密的连接&#xff0c;从而实现外部用户通过SSH隧道访问内网中的数据库。具体步骤如下&#xff1a; 使用端…

【2023年11月第四版教材】《第2章-信息技术发展之新一代信息技术及应用(第四部分)》

第2章-信息技术发展之新一代信息技术及应用&#xff08;第四部分&#xff09; 5 新一代信息技术及应用5.1 物联网5.2 云计算5.3 大数据 5 新一代信息技术及应用 5.1 物联网 要素具体内容存储技术定义物联网是指通过信息传感设备,按约定的协议将任何物品与互联网相连接&#x…

前端处理后端返回的数据中有\n\n字样的换行符标识

后端返回的数据&#xff1a; 上面圈着的部分就是\n&#xff0c;前端需要将数据进行换行&#xff0c;对于这类型的数据&#xff0c;在前端页面是需要进行稍微处理才能正常显示。如果没有经过处理&#xff0c;那么内容是不会在有换行符的位置进行换行显示的 解决办法1&#xff1…

滑动窗口(全面清晰/Java)

数组模拟单调队列 分析 以k3举例&#xff1a; (1)利用单调队列的性质&#xff1a; <1>最小值&#xff1a;确保队列单调递增&#xff0c;处理后&#xff0c;队头即是最小值。 <2>最大值&#xff1a;确保队列单调递减&#xff0c;处理后&#xff0c;队头即是最大值…

http历史版本

1&#xff0c;HTTP0.9 最早的http版本&#xff0c;后来才被定义为0.9版本。 这时候通信采用的是纯文本格式&#xff1b; 只支持get请求&#xff0c;且在服务器响应之后就关闭连接&#xff1b; 没有请求头的概念&#xff0c;功能比较简单。 2&#xff0c;HTTP1.0 这个版本增…