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 ...
随机推荐
- cocos Studio 特效文件播放失败
可能是json文件中file的Path不对引起的.因为生成的path是根据文件在项目中的path生成的,即当此特效不是项目的一级目录时,会带有项目中上级目录,导致找不到文件(不会报错). ccs.lo ...
- 记录 shell学习过程(4)for 循环
1. for in ` #seq 生成从1到10 如果生成从10到1则写作 seq 10 -1 1 do echo $i done for in 也可以循环出字符串 for i in where is ...
- webrtc vp8与h264 sdp文件解读
参考地址:https://blog.csdn.net/zhangjikuan/article/details/27367437, https://www.cnblogs.com/idignew/p/7 ...
- 1、spring与springmvc父子容器
转载于http://www.tianshouzhi.com/api/tutorials/spring 1.0 spring与springmvc父子容器 1.spring和springmvc父子容器概念 ...
- JQ 遍历--(祖先,后代,同胞,过滤)
祖先 <style> .one,.one *{ display: block; border: 2px solid lightgrey; color: lightgrey; padding ...
- html5 游戏源码下载网站,你值得拥有!
在游戏开发的学习或工作中,利用完好的游戏源码可以事半功倍,不仅可以逆向学习开拓思维,也可以大大减少设计周期. HTML5是构建Web内容的一种语言描述方式. HTML5是Web中核心语言HTML的规范 ...
- 用户 'sa' 登录失败。该用户与可信 SQL Server 连接无关联'。错误代码:18452 解决办法
原文:https://blog.csdn.net/wuxianwei/article/details/6330270 SQLSERVER 2005采用'SQLSERVER身份验证'去登录, 出错的原因 ...
- Linux - Shell - cut: 低配 awk
概述 简述 shell 命令行工具 cut 背景 偶尔需要用 awk 来筛选特定的列 awk 很是强大 但是强大的背后, 却伴随着复杂 其实同样的功能, awk 也没有复杂多少 如果是 简单的任务, ...
- 洛谷 P1659 [国家集训队]拉拉队排练(Manacher)
题目链接:https://www.luogu.com.cn/problem/P1659 思路: 首先跑一遍Manacher,用$cnt_i$记录长为$i$的回文串有多少个. 所记录的$cnt$并不是最 ...
- 使用@RunWith(SpringJUnit4ClassRunner.class)进行单元测试时 报错 和 java.lang.NoSuchMethodError的解决方法
1 使用@RunWith(SpringJUnit4ClassRunner.class)进行单元测试时,需要junit高版本和spring-test的高版本才支持,junit需要4.0以上的,sprin ...