HDU2444 The Accomodation of Students(二分匹配 匈牙利算法)

【代码超详解】HDU 2444 The Accomodation of Students(二分图的判定 + 匈牙利算法求二分图的最大匹配) 一、题目描述 二、算法分析说明与代码编写指导 匈牙利算法及二分图的判定 三、AC 代码 #include<cstdio> #include<vector> #include<bitset> #include<algorithm> #pragma warning(disable:4996) using namespace std; const uns... 阅读详情

Problem Description
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

Calculate the maximum number of pairs that can be arranged into these double rooms.
 

Input
For each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.

Proceed to the end of file.

 

Output
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
 

Sample Input
4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6
 

Sample Output
No 3
 

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 205

int a[N][N],vis[N],link[N];
int n,m;

bool bfs()
{
	int i,j;
	queue<int>q;
	for(i=1;i<=n;i++)
	{
		if(vis[i]!=-1) continue;

		vis[i]=1;
		q.push(i);
		while(!q.empty())
		{
			int cur=q.front();
			q.pop();
			for(j=1;j<=n;j++)
				if(a[cur][j])
			{
				if(vis[j]==vis[cur]) return false;
				if(vis[j]==-1)
				{
					vis[j]=(vis[cur]+1)%2;
					q.push(j);
				}
			}
		}
	}
	return true;
}

bool dfs(int x)
{
	int i,j;
	for(i=1;i<=n;i++)
		if(a[x][i]&&!vis[i])
	{
		vis[i]=1;
		if(link[i]==0||dfs(link[i]))
		{
			link[i]=x;
			return true;
		}
	}
	return false;
}

int main()
{
	int i,j;
	while(~scanf("%d%d",&n,&m))
	{
		memset(a,0,sizeof(a));
	    int e,s;
	    while(m--)
		{
			scanf("%d%d",&s,&e);
			a[e][s]=a[s][e]=1;
		}
		memset(vis,-1,sizeof(vis));

		if(!bfs())
		{
			printf("No\n");
			continue;
		}

		int ans=0;
		memset(link,0,sizeof(link));

		for(i=1;i<=n;i++)
		{
			memset(vis,0,sizeof(vis));
			if(dfs(i)) ans++;
		}
		printf("%d\n",ans/2);
	}

	return 0;

}




伺服电机三环控制实战:从参数整定到系统优化 本文以实战视角,深入解析伺服电机三环控制(位置环、速度环、电流环)的参数整定与系统优化全流程。文章摒弃复杂理论,通过生动的比喻和分步调试指南,详细阐述了从电流环快速响应、速度环平滑调节到位置环精准定位的调试方法,并提供了常见问题排查与进阶优化技巧,帮助工程师有效提升伺服系统性能与稳定性。 阅读详情

相关推荐

Arxml文件可视化:当汽车软件架构遇见交互式设计工具

本文探讨了Arxml文件在汽车电子系统开发中的可视化技术,结合AUTOSAR架构标准,介绍了如何利用现代Web技术构建交互式设计工具。通过三维组件关系图、动态数据流展示和版本对比等功能,显著提升开发效率和架构评审质量,适用于MBD和ASPICE开发流程。

weixin_29050271的博客 427

HDU - 2444——The Accomodation of Students【二分图】

The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8263    Accepted Submission(s): 3649Problem DescriptionThere are a g...

xiang_hehe的博客 268

MATPOWER下载安装教程

将MATPOWER文件复制在C:\Program Files (x86)\MATLAB\R2016b\bin目录下。原来的窗口中多了很多文件路径,点击【保存】,再点击【关闭】选择所要添加的MATPOWER文件夹,点击【选择文件夹】二、将MATPOWER放在bin目录下。找到菜单栏的【设置路径】点击【添加并包含子文件】一、从官网下载后解压。

weixin_47365903的博客 2万+

The Accomodation of Students(二分匹配+二分图判定)

The Accomodation of Students(二分匹配+二分图判定) There are a group of students. Some of them may know each other, while others don’t. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. Now you are gi

Anthcony的博客 355

ios生成海报使用dom-to-image中toPng报错isTrusted:true

主要是ios中safari浏览器解析错误,引起的报错,主要引起原因toPng会调用toSvg,toSvg直接将html标签内容通过svg中foreignObject标签直接包裹,然后转base64, 通过data:image/svg+xml;解决办法:二次生成(调用两次),判断ios or 安卓,生成第一次生成,不用参数,例如:scale: 3,在第二次生成再进行清晰度操作,避免多次生成,引起性能消耗。以上种种很多案例,使用canvase进行转换的,标签丢失,直接toPng版本的,兼容性问题。

weixin_44601570的博客 2105

数据分析项目:电商用户行为分析系统

本报告基于某电商平台用户行为数据(采样10%),通过清洗、探索性分析及多维度建模,旨在挖掘用户行为特征、商品销售规律及用户价值分层,为运营策略优化提供数据支持。原始数据字段:用户ID、商品ID、类目ID、行为类型、时间戳。长尾商品(购买量低)可考虑捆绑销售或限时折扣,提升动销率。长尾商品(购买量低)可考虑捆绑销售或限时折扣,提升动销率。,少数高活跃用户贡献大部分行为,多数用户行为次数较少。

Nervousr的博客 1700

HDU2444(最大匹配)

题意:有n个人,和m种关系,代表a和b认识。要求将n个人放入到两个集合中,每个集合中的人互不认识,A集合中的人找B集合的人住房间,两个人一间,且两个人必须认识,问你最多能住多少间房间。 思路:先确定A、B集合中各有哪些人,我们可以用个数组表示每个人在哪个集合中,若有重复,则输出No,反之再找最大匹配。 #include #include #include #include #inclu

慕枫幕影的专栏 1418

Pluto SDR新手避坑指南:从环境配置到第一个MATLAB收发程序(附完整代码)

本文为Pluto SDR新手提供全面的避坑指南,从硬件连接到MATLAB环境配置,再到第一个信号收发程序的实现。详细介绍了驱动安装、常见问题排查以及关键参数设置,帮助用户快速上手软件定义无线电(SDR)技术,并附有完整代码示例。

weixin_33168937的博客 324

The Accomodation of Students——判定二分图并求最大匹配

Problem Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. Now you are given all pairs of stu

小白之比白更白的博客 246

LLaMA-Factory微调教程2:命令行sft微调

本文介绍了如何使用llama-factory项目在本地运行Qwen2-7B大语言模型进行对话。首先通过llamafactory-cli env查看环境配置,然后使用git lfs下载Qwen2-7B模型。创建配置文件qwen2_7b_instruct.yaml,修改模型路径和模板参数。最后通过llamafactory-cli chat命令启动对话服务,加载模型配置文件和tokenizer,完成环境初始化后即可进行交互式对话。

Cachel Wood的博客 8814

Vue3 - 最新实现在线预览Excel文件(xls/xlsx)多种方案,解析打开excel电子表格支持服务器链接在线URL形式、调用后端接口获取二进制文件流、文件地址(vue3实现excel预览)

vue3,预览excel文件,xlsx,xls,vue预览Excel表格,vue在线编辑xls文件,预览和编辑,vue3如何新开一页浏览器打开查看,手机h5页面打开在线EXCEL文档,在弹框中加载预览,在指定模态框中显示,基于Vue的Excel文件预览,加载失败白屏,报错没反应,详细示例代码教程,文件预览下载,nuxt3,excel预览插件,xlsx插件预览,纯前端vue中预览excel表格,接口调用的文件如何预览查看,文件url地址,vue移动端实现excel在线预览,vue移动端H5网页excel预览,

🏆 前端领域优质创作者 1万+

HDU 2444 The Accomodation of Students(最大二分匹配(匈牙利算法)+二分图判断->模板题目)

这道题首先判断一下图是不是二部图,如果是的话,就进行二分匹配 代码: #include #include #include using namespace std; const int N = 300; int n, m; int color[N]; int g[N][N], linker[N]; bool used[N]; bool dfs( int u ) { int

AClion的专栏 823

【DaVinci Developer工具实战】03 -导入xml文件

本文档描述了 DaVinci Developer 导入数据的的具体功能 ,它适用于导入AUTOSAR SWC 模板【AUTOSAR XML 文件】 文件。

1990

HDU 2444 The Accomodation of Students 匈牙利算法

http://acm.hdu.edu.cn/showproblem.php?pid=2444 There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But ...

xiji333的博客 220

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

hdu 2444-The Accomodation of Students(二分匹配)

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 609

蓝色污点的专栏 1831

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Hdu 2444 The Accomodation of Students

题目: There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each o...

zamaochick的博客 178

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

HDU 2444 The Accomodation of Students

匈牙利算法 https://blog.youkuaiyun.com/sixdaycoder/article/details/47680831 将找增广路径转化为找到出发点集合m里的另一点,match其实只对二分图非起点集的另一半有效 但是这道题并不明确哪一部分是起点集,我们是全部都搜,所以会重复,最后结果除以二 还有就是染色法,一开始是边输入边赋0,1。。。 个人感觉染色法两个关键,1,一定要是连...

qq_43331910的博客 303

The Accomodation of Students HDU - 2444

题目链接 大意:给n个关系,其中有m对人是相互认识的,如果能把这n个人分成两组,每组内的人相互不认识,则问最大匹配数是多少 思路:看懂题的话就是二分图裸题,但一开始要先判断是否是二分图,用bfs染色 代码: #include<bits/stdc++.h> #define maxn 205 using namespace std; vector<int>edge[maxn...

qq_43697256的博客 162

二分图判定+最大匹配+例题HDU2444 The Accomodation of Students

注:本文参考博客链接:二分图大合集 二分图定义: 二分图也称二部图,是图论里的一种特殊模型,也是一种特殊的网络流。其最大的特点在于,可以将图里的顶点分为两个集合,且集合内的点没有直接关联。 二分图判定: 如果某个图为二分图,那么它至少有两个顶点,且其所有回路的长度均为偶数(或者回路中结点个数为偶数),任何无回路的的图均是二分图。 判断二分图的方法: 判断二分图方法:用染色法,把图中的点染成黑色和白色。 首先取一个点染成白色,然后将其相邻的点染成黑色,如果发现有相邻且同色的点,那么就退出,可知这个图并非二分图

the best 的博客 906

HDU 2444 The Accomodation of Students 【二分图匹配】【匈牙利算法】【bfs判断二分图】

The Accomodation of Students   Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8147    Accepted Submission(s): 3605     Problem Descriptio...

Emiya丶的博客 435

hdu 2444 The Accomodation of Students(判断二分图+匈牙利算法)

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4085    Accepted Submission(s): 1873 Problem Descri

Where amazing happens 459

hdu 2444 The Accomodation of Students 判断是否为二分图+最大匹配

Problem Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. Now you are given all pairs of stu

Lionel 1021

HDU 2444 The Accomodation of Students(二分图判断&&匈牙利算法)

Problem Description There are a group of students. Some of them may know each other, while others don’t. For example, A and B know each other, B and C know each other. But this may not imply that ...

←_← 274

HDU - 2444 The Accomodation of Students(二分图判断加匈牙利算法)

There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other....

vocaloid01的博客 226

hdu oj 2444 The Accomodation of Students-二部图

二部图

DylanJoe的博客 381

hdu 2444 The Accomodation of Students(判二分+最大匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2444

要去相信,没有到不了的明天 736
上一篇: HDU1251 统计难题(字典树)
下一篇: poj1740 A New Stone Game(博弈)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值