HDU 2444 The Accomodation of Students【二分图最大匹配问题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444
题意:首先判断所有的人可不可以分成互不认识的两部分。如果可以分成 ,则求两部分最多相互认识的对数。
思路:二分图最大匹配问题。先BFS判断是否为二分图,再用匈牙利算法算最大匹配量
关于匈牙利算法:https://blog.csdn.net/CillyB/article/details/55511666
代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
int map[205][205];
int vis[205];
int girl[205];
int n, m;
int find(int x)
{
for(int y = 1; y <= n; y++)
{
if(map[x][y] && vis[y] == 0)
{
vis[y] = 1;
if(girl[y] == 0 || find(girl[y]))
{
girl[y] = x;
return 1;
}
}
}
return 0;
}
bool solve()
{
queue<int>q;
q.push(1);
vis[1] = 1;
while(!q.empty())
{
int x = q.front();
q.pop();
for(int i = 1; i <= n; i++)
{
if(map[x][i])
{
if(vis[i] == 0)
{
if(vis[x] == 1)
vis[i] = 2;
else
vis[i] = 1;
q.push(i);
}
else if(vis[i] == vis[x])
return false;
}
}
}
return true;
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
map[i][j] = map[j][i] = 0;
vis[i] = 0;
}
for(int i = 1; i <= m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
map[a][b] = map[b][a] = 1;
}
if(n == 1 || !solve())
{
cout << "No" << endl;
continue;
}
else
{
int ans = 0;
memset(girl,0,sizeof(girl));
for(int i = 1; i <= n; i++)
{
memset(vis, 0, sizeof(vis));
ans += find(i);
}
cout << ans / 2 << endl;
}
}
}
HDU 2444 The Accomodation of Students【二分图最大匹配问题】的更多相关文章
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- 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 ...
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- HDU 2444 The Accomodation of Students二分图判定和匈牙利算法
本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students (判断二分图,最大匹配)
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- springcloud--ribbo(负载均衡)
ribbo:是Netflix公司开源的一个负载均衡的项目,是一个客户端负载均衡器,运行在客户端上. 实际运用案例(基于springcloud入门案例): 一.新建Module:springcloud- ...
- Exchange Server备份与恢复
本文档描述了Exchange 2003.Exchange Server 2007/2010的备份与恢复操作,涉及的内容包括: 1.使用NTBackup 备份与恢复Exchange 2007/2003 ...
- 根据上传的MultipartFile通过springboot转化为File类型并调用通过File文件流的方法上传特定服务器
@PostMapping("uploadExcel") public ResponseObj uploadExcel(@RequestParam("excelFile ...
- 解决:Server IPC version 9 cannot communicate with client version 4
使用idea的maven项目运行mapreduce程序Server IPC version 9 cannot communicate with client version 4 原因: Java初始化 ...
- x86平台inline hook原理和实现
概念 inline hook是一种通过修改机器码的方式来实现hook的技术. 原理 对于正常执行的程序,它的函数调用流程大概是这样的: 0x1000地址的call指令执行后跳转到0x3000地址处执行 ...
- 八十三、SAP中的ALV创建之二,ALV相关的类型池定义
一.与ALV相关的类型都是在TYPE-POOLS:SLIS中.我们来到SE11 二.常用的定义有fieldca和layout等,用于显示字段,和控制信息数据等. 三.我们以VBAK表为例,用ALV输出 ...
- 033-PHP取1-100的随机数
<?php // 生成一个随机数 // 从1到100中取得随机数 for ($index = 0; $index < 100; $index++) { $number = (rand() ...
- 143-PHP printf函数
<?php $num=123.456; //定义一个浮点数变量 printf('以整数形式输出:%d',$num); //格式化为有符号十进制整数后输出 ?> <?php $num= ...
- Hadoop组件详解(随缘摸虾)
1.1. Hadoop组成: Hadoop = hdfs(存储) + mapreduce(计算) + yarn(资源协调) + common(工具包) + ozone(对象存储) + submarin ...
- Java工程师面试题
1. J2EE 是什么?它包括哪些技术?解答:从整体上讲,J2EE 是使用 Java 技术开发企业级应用的工业标准,它是 Java 技术不断适应和促进企业级应用过程中的产物.适用于企业级应用的 J2E ...