HDU-1213 How Many Tables

这篇文章介绍了一个关于计算Ignatius生日聚会所需最少餐桌问题的算法,根据朋友之间的相互认识关系来确定。输入为朋友数量和认识关系的数量,输出为最少餐桌数。核心在于合并已知熟识的朋友群,避免陌生人共桌。

How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 73145    Accepted Submission(s): 36926


 
Problem Description
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 Input
 
  
2 5 3 1 2 2 3 4 5 5 1 2 5
 
 
Sample Output
 
  
2 4
 
 
Author
Ignatius.L
 
 
Source
 
 
Recommend

 

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int p[1010];

int find(int x)
{
	if(p[x]!=x)
	return p[x]=find(p[x]);
	return x;
}
int merge(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(p[fx]!=p[fy])
    {
        p[fx]=fy;
        return 1;
	}
	return 0;
}
int main()
{
	int t;
	int m,n;
	int x,y;
	int ans;
	
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		ans=n;
		for(int i=1;i<=n;i++)p[i]=i;
		for(int i=1;i<=m;i++)
		{
			cin>>x>>y;
			if(merge(x,y))
			ans-=1;
		}
		cout<<ans<<endl;
	}
	return 0;//
}

 

MacBook上安装VSCode并配置Python开发环境可按以下步骤进行: ### 安装VSCode Mac安装VSCode非常简单,直接从官网下载,解压之后将其移动到应用程序目录即可[^4]。 ### 安装Python 可参考相关资料如“MacOS配置Python开发环境Pycharm的详细步骤(完整版)https://blog.youkuaiyun.com/weixin_45571585/article/details/128606516?spm=1001.2014.3001.5502”中的Python安装步骤进行安装[^2]。 ### 通用配置VSCode - **中文配置**:默认环境下是英文的,在扩展部分搜索Chinese进行安装安装完成后,会提示重启,按照步骤执行即可将界面语言更换为中文[^4]。 - **关闭更新**:为避免更新后出现如无法连接远程的情况,可将更新部分关掉[^4]。 - **字体大小**:字体部分有两方面需要修改,个是编辑器的显示字体大小,另个是整个窗口的字体,通过在设置中搜索zoom来设置,实际上修改这个zoom会影响编辑器的字体大小,可根据需求来调整[^4]。 - **远程支持**:若在远程服务器上进行开发,需要增加远程的扩展支持,搜索remote这个插件进行安装安装后连接远程服务器,选择目录后进行执行[^4]。 - **主题**:在扩展部分直接搜索theme,选择合适的主题,例如github的深色主题[^4]。 ### Python支持配置 - **安装扩展**:需要安装python、pylance、flake8、yapf等扩展。安装好之后,还需要在服务器安装相关依赖,若服务器上主要用的是python3,执行命令`pip3 install flake8 yapf`。然后在服务器端的VSCode配置文件中添加以下内容: ```json { "flake8.args": [ "--max-line-length=248" ], "python.defaultInterpreterPath": "python3", "[python]": { "editor.formatOnSaveMode": "file", "editor.formatOnSave": true, "editor.defaultFormatter": "eeyore.yapf", "editor.formatOnType": false } } ``` 这样平时写些小程序基本够用,当然语法检查器格式化器还有别的配置,也可参考他人推荐[^4]。 - **选择Python解释器**:安装完解释器之后,停留在VSCode界面按F1,然后输入“Python:Select Interpreter”来选择合适的Python解释器[^2]。 ### 环境验证 可以新建Python工程,在VSCode界面选择“add a new workspace”添加工作区,新建个`test.py`文件并编写脚本(如经典的“Hello World”程序)。运行时,选择运行环境,若VSCode检测到了如conda环境等,可选择相应环境执行脚本,若能成功输出结果,则说明环境配置成功[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值