HDU2444 The Accomodation of Students —— 二分图最大匹配
题目链接:https://vjudge.net/problem/HDU-2444
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7328 Accepted Submission(s): 3270
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.
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.
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
3
题解:
任务1:能否把这些人分成两组,且在每一组内,所有人互不相识(即两点间没有边直接相连)?
任务2:求最大匹配数, 直接用hungary()算法。
1.由于要把所有点分成两组,所以我们可以用两种颜色,对整幅图进行染色。规定:相邻两点间的颜色不同,然后把颜色相同的归为一组。
2.对于没有被染色的点u,对其进行染色,然后遍历所有与之相连的点v,如果点v没有被染色,则对其进行访问,染上另外一种颜色;如果点v已经被染色,则根据点u和点v的的染色情况来判断是否有冲突:
3.如果颜色相同,即把他们放在同一组,但他们是相互认识的,不能放在同一组,所以产生了冲突;如果颜色不同,则他们被分在了两组,符合要求。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = +; int n;
char a[MAXN][MAXN];
int M[MAXN][MAXN], link[MAXN];
bool vis[MAXN]; bool dfs(int u)
{
for(int i = ; i<=n; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=n; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} int col[MAXN];
bool Color(int u, int c) //染色,如果有冲突,则返回true
{
col[u] = c;
for(int i = ; i<=n; i++)
if(M[u][i])
{
if(col[i]==col[u]) return true; //与之前访问过的点相连,且为同色,则有冲突。
if(col[i]==- && Color(i, !c)) return true; //如果没有没有访问过,则对其染色。
}
return false;
} int main()
{
int m;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(M, false, sizeof(M));
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
M[u][v] = M[v][u] = true;
} bool flag = false;
memset(col, -, sizeof(col));
for(int i = ; i<=n; i++) //染色
if(col[i]==-)
flag = flag|Color(i, ); if(flag)
{
printf("No\n");
continue;
} int cnt = hungary();
printf("%d\n", cnt/);
}
}
HDU2444 The Accomodation of Students —— 二分图最大匹配的更多相关文章
- HDU2444 The Accomodation of Students(二分图最大匹配)
有n个关系,他们之间某些人相互认识.这样的人有m对.你需要把人分成2组,使得每组人内部之间是相互不认识的.如果可以,就可以安排他们住宿了.安排住宿时,住在一个房间的两个人应该相互认识.最多的能有多少个 ...
- 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 ...
随机推荐
- Mysql学习总结(44)——Linux下如何实现mysql数据库每天自动备份定时备份
概述 备份是容灾的基础,是指为防止系统出现操作失误或系统故障导致数据丢失,而将全部或部分数据集合从应用主机的硬盘或阵列复制到其它的存储介质的过程.而对于一些网站.系统来说,数据库就是一切,所以做好 ...
- 局部二值模式(Local Binary Patterns)纹理灰度与旋转不变性
Multiresolution Gray Scale and Rotation Invariant Texture Classification with Local Binary Patterns, ...
- 自定义PHP错误报告处理方式
<?php //在php中注册一个函数, 来处理错误报告, 而不按原来的方式处理了 set_error_handler("myerrorfun"); $mess = &quo ...
- CodeForces - 754B Ilya and tic-tac-toe game
简单搜索 判断是否能在最后一步下棋得到胜利 问题转化为 是否有可以胜利的x的摆法 那么就只有两种情况 1.有两个x相连 并且 在端点还有.可以落子 那么就可以在最后一步 胜利 2.两个x中间恰好有一个 ...
- resin web项目的 编码问题
问题描述: 服务器迁移,迁移以后Linux系统编码由 UTF-8 变成了GBK !!! 导致在resin 中运行java web项目,调用 http 接口,解析http 接口的返回内容 如:xml 时 ...
- Linux 端口开放
Linux(CentOS): 系统缺省值为32768-61000. 修改方法: 在/etc/sysctl.conf中,增加以下配置:(开放20000-50000为完成端口) net.ipv4.ip_l ...
- iOS-bug·Failed to connect to github-production-release-asset-2e65be.s3.amazonaws.com port 443: Operation time out
在首次运行 react naticve 项目时, 遇到了如下的问题: Failed to connect to github-production-release-asset-2e65be.s3.am ...
- OC温习四:数组
/** arrayByAddingObject -- 往数组A添加一个数据,返回一个数组的形式,即必须有一个数组来接受 */ NSArray *array = [NSArray arrayWithOb ...
- express 写接口
实例下载:百度云盘免密码 (): 指注释 一.准备工作 1.安装express npm install express -g npm install express-generator -g 2.初始 ...
- 洛谷——P1451 求细胞数量
P1451 求细胞数量 题目描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数.(1<=m,n<=10 ...