HDU2444 The Accomodation of Students(二分图最大匹配)
有n个关系,他们之间某些人相互认识。这样的人有m对。
你需要把人分成2组,使得每组人内部之间是相互不认识的。
如果可以,就可以安排他们住宿了。安排住宿时,住在一个房间的两个人应该相互认识。
最多的能有多少个房间住宿的两个相互认识。
先是要判断是否为二部图,然后求最大匹配。
学习一下模板~
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=;
vector<int> g[maxn];
int linker[maxn];
bool used[maxn];
int uN;
int matchs[maxn];
int cnt[maxn];
bool dfs (int u) {
int i;
for (i=;i<g[u].size();i++) {
int v=g[u][i];
if (!used[v]) {
used[v]=true;
if (linker[v]==-||dfs(linker[v])) {
linker[v]=u;
return true;
}
}
}
return false;
}
int hungary () {
int res=;
int u;
memset(linker,-,sizeof(linker));
for (u=;u<=uN;u++) {
memset(used,false,sizeof(used));
if (dfs(u)) res++;
}
return res;
}
bool judge (int x,int y) {
int i;
for (i=;i<g[x].size();i++) {
if (cnt[g[x][i]]==) {
cnt[g[x][i]]=-y;
matchs[g[x][i]]=true;
if (!judge(g[x][i],-y)) return false;
}
else if (cnt[g[x][i]]==y) return false;
}
return true;
}
bool matched () {
int i;
memset(matchs,false,sizeof(matchs));
for (i=;i<=uN;i++) {
if (g[i].size()&&!matchs[i]) {
memset(cnt,,sizeof(cnt));
cnt[i]=-;
matchs[i]=true;
if (!judge(i,-)) return false;
}
}
return true;
}
int main () {
int m;
int i;
int u,v;
while (~scanf("%d %d",&uN,&m)) {
for (i=;i<=uN;i++)
if (g[i].size()) g[i].clear();
while (m--) {
scanf ("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
if (matched()) printf ("%d\n",hungary()/);
else printf ("No\n");
}
return ;
}
HDU2444 The Accomodation of Students(二分图最大匹配)的更多相关文章
- HDU2444 The Accomodation of Students —— 二分图最大匹配
题目链接:https://vjudge.net/problem/HDU-2444 The Accomodation of Students Time Limit: 5000/1000 MS (Java ...
- hdu_2444The Accomodation of Students(二分图的判定和计算)
hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...
- HDU2444 :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 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- HDU2444 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 (二分图存在的判定以及最大匹配数)
There are a group of students. Some of them may know each other, while others don't. For example, A ...
- hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)
//推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图.否则是二分图. //推断回路奇偶性:把相邻两点染成黑白两色.假设相邻两点出现颜色同样则存在奇数回路. 也就是非二分图. # incl ...
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- HDU2444 The Accomodation of Students【匈牙利算法】
题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...
随机推荐
- SequoiaDB巨杉数据库入门:快速搭建流媒体服务器
使用SequoiaDB的分布式文件系统搭建流媒体服务器 介绍 如今使用移动互联网的年轻人开始越来越多使用短视频展示自我,而流媒体则是支撑在线视频播放的核心技术.当我们开始构建流媒体站点时,往往面临最大 ...
- 08 部署nginx web服务器(转发fastDFS请求)
先准备两个文件: fastdfs-nginx-module-master.tar.gz:fastDFS nginx安装包 nginx-1.17.3.tar.gz:nginx安装包 注:这两个包文件要匹 ...
- AcWing 278. 数字组合 求方案数目
//M看成背包容量,把每个数看成一个物品,Ai看成是体积 //目标:求总体积恰好为M的方案数目 #include <iostream> using namespace std; ; int ...
- IIS支持json、geojson文件
最近在搞asp.net + openlayers. 其中openlayer有个数据源支持 .geojson 数据,但是怎么测试都不能成功.同样的数据拿到php下就能成功显示. 搓. 在网上漫无目的的搜 ...
- xrdp---远程桌面连接
xrdp is an Open Source Remote desktop Protocol server, which allows you to RDP to your Linux server ...
- iterations 快捷键
原帖:https://blog.csdn.net/Soinice/article/details/83505198 为了防止删除备份的. iterations 快捷键 Live Templates 其 ...
- 在虚拟机中使用Git
自己如何从安装虚拟机到使用git进行项目代码版本管理的部分教程因为是自学所以没有好的教程只能自己进行百度,网上的教程太多了但都是只是一个模块没有从头到尾详细的教程,我们如果有个详细的教程本来只需花很少 ...
- vuecli+axios的post请求传递参数异常
大多数的web服务器只能识别form的post的请求,即请求头Content-Type为’application/x-www-form-urlencoded‘ axios.defaults.heade ...
- C++-HDU1000,1001,1002-格式是真的坑
#include <cstdio> int main(){ for(int a,b;~scanf("%d%d",&a,&b);printf(" ...
- 2019牛客多校第一场E ABBA dp
ABBA dp 题意 给出2(N+M)个AB字符,问能构造出N个AB子序列和M个BA子序列组成的2*(n+m)的序列种类有多少 思路 碰到计数构造类的题目,首先要去找到判断合法性的条件,即什么情况下合 ...