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 ( ...
随机推荐
- 官网英文版学习——RabbitMQ学习笔记(十)RabbitMQ集群
在第二节我们进行了RabbitMQ的安装,现在我们就RabbitMQ进行集群的搭建进行学习,参考官网地址是:http://www.rabbitmq.com/clustering.html 首先我们来看 ...
- 038-PHP向返回的闭包函数实例中,传递外部变量参数
<?php # 向返回的闭包函数实例中,传递外部变量参数 # 直接调用将不会输出$txt的内容 function demo(){ $txt = '我爱PHP'; # 1.function()内的 ...
- springboot - 映射HTTP Response Status Codes 到 静态 HTML页面
1.总览 2.代码 1).pom.xml <dependencies> <dependency> <groupId>org.springframework.boot ...
- 在登陆退出时候使用Vuex
1.登陆的时候,在登陆模块请求接口,然后获取一个access_token,获取用户权限.保存到缓存里面. 2.退出的时候,请求退出接口,把缓存里面的access_token清除. 一旦要在登陆里面做一 ...
- PHP购物网站
我使用的phpsteam经常用着用着就闪退,所以做起来挺麻烦的.里面的代码有抄袭借鉴网上的代码,就是那个php做购物网站点击量最高的那个. 但是我很多代码也是自己写的不和其相同. PHP是一门选修课, ...
- Windows2008R2安装iis和iis下搭建web服务器(9.18 第七天)
IIS internet information services 互联网信息服务微软开发的运行在windows中的互联网服务,提供了web.ftp.smtp服务 Windows server 200 ...
- 每天一点点之vue框架开发 - 如何在一个页面调用另一个同级页面的方法
使用场景: 页面分为header.home.footer三部分,需要在home中调用header中的方法,这两个没有相互引入 官方给出方法: api传送门 在项目中实现: 1.首先同一个vue实例来调 ...
- vue.js实现自定义输入分页
效果如下: html: <input type="text" value="1" v-model="page.page_my_selected& ...
- 【LeetCode】解数独
做题之前先复习下[STL中的Tuple容器] 我们知道,在Python中,大家都知道tuple这个概念,是一个只读的元素容器,容器内的元素数据类型可以不同,而在CPP中大部分的容器只能储存相同数据类型 ...
- zabbix_server
http://www.linuxidc.com/Linux/2014-11/109909.htm [root@localhost zabbix]# service iptables stop 关闭i ...