POJ1469 COURSES 【二分图最大匹配·HK算法】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17777 | Accepted: 7007 |
Description
- every student in the committee represents a different course (a student can represent a course if he/she visits that course)
- each course has a representative in the committee
Input
P N
Count1 Student1 1 Student1 2 ... Student1 Count1
Count2 Student2 1 Student2 2 ... Student2 Count2
...
CountP StudentP 1 StudentP 2 ... StudentP CountP
The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses �from course 1 to course P,
each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you抣l find the Count i students, visiting the course, each
two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N.
There are no blank lines between consecutive sets of data. Input data are correct.
Output
Sample Input
2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1
Sample Output
YES
NO
Source
题意:有P门课,N个学生,每门课仅仅能相应一个人,可是单个人能够相应多门课。求最大匹配是否等于P。
题解:匈牙利也能够解,看到书上介绍了这个HK算法,时间复杂度要更低,于是尝试了下,可是...写起来真是太麻烦了。
#include <stdio.h>
#include <string.h>
#include <queue> #define maxn 305
#define maxp 105
#define maxm maxn * maxp
#define inf 0x3f3f3f3f int head[maxp], id, p, n, dis;
struct Node {
int v, next;
} E[maxm];
int dx[maxp], dy[maxn], cx[maxp], cy[maxn];
bool visy[maxn]; void AddEdge(int u, int v) {
E[id].v = v;
E[id].next = head[u];
head[u] = id++;
} void GetMap() {
int k, v, i; id = 0;
scanf("%d%d", &p, &n);
memset(head, -1, sizeof(int) * (p + 1));
for(i = 1; i <= p; ++i) {
scanf("%d", &k);
while(k--) {
scanf("%d", &v);
AddEdge(i, v);
}
}
} bool searchPath() {
std::queue<int> Q;
int i, u, v; dis = inf;
memset(dx, 0, sizeof(int) * (p + 1));
memset(dy, 0, sizeof(int) * (n + 1));
for(i = 1; i <= p; ++i) {
if(!cx[i]) Q.push(i);
}
while(!Q.empty()) {
u = Q.front(); Q.pop();
if(dx[u] > dis) break;
for(i = head[u]; i != -1; i = E[i].next) {
if(!dy[v = E[i].v]) {
dy[v] = dx[u] + 1;
if(!cy[v]) dis = dy[v];
else {
dx[cy[v]] = dy[v] + 1;
Q.push(cy[v]);
}
}
}
}
return dis != inf;
} int findPath(int u) {
int i, v;
for(i = head[u]; i != -1; i = E[i].next) {
if(!visy[v = E[i].v] && dx[u] + 1 == dy[v]) {
visy[v] = 1;
if(dy[v] == dis && cy[v]) continue;
if(!cy[v] || findPath(cy[v])) {
cy[v] = u; cx[u] = v;
return 1;
}
}
}
return 0;
} int MaxMatch() {
int ans = 0, i;
memset(cx, 0, sizeof(int) * (p + 1));
memset(cy, 0, sizeof(int) * (n + 1));
while(searchPath()) {
memset(visy, 0, sizeof(bool) * (n + 1));
for(i = 1; i <= p; ++i)
if(!cx[i]) ans += findPath(i);
}
return ans;
} void Solve() {
printf(MaxMatch() == p ? "YES\n" : "NO\n");
} int main() {
// freopen("stdin.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
GetMap();
Solve();
}
return 0;
}
POJ1469 COURSES 【二分图最大匹配·HK算法】的更多相关文章
- 二分图最大匹配:匈牙利算法的python实现
二分图匹配是很常见的算法问题,一般用匈牙利算法解决二分图最大匹配问题,但是目前网上绝大多数都是C/C++实现版本,没有python版本,于是就用python实现了一下深度优先的匈牙利算法,本文使用的是 ...
- 51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题
题目: 题目已经说了是最大二分匹配题, 查了一下最大二分匹配题有两种解法, 匈牙利算法和网络流. 看了一下觉得匈牙利算法更好理解, 然后我照着小红书模板打了一遍就过了. 匈牙利算法:先试着把没用过的左 ...
- "《算法导论》之‘图’":不带权二分图最大匹配(匈牙利算法)
博文“二分图的最大匹配.完美匹配和匈牙利算法”对二分图相关的几个概念讲的特别形象,特别容易理解.本文介绍部分主要摘自此博文. 还有其他可参考博文: 趣写算法系列之--匈牙利算法 用于二分图匹配的匈牙利 ...
- POJ 1469 COURSES 二分图最大匹配 二分图
http://poj.org/problem?id=1469 这道题我绝壁写过但是以前没有mark过二分图最大匹配的代码mark一下. 匈牙利 O(mn) #include<cstdio> ...
- 二分图最大匹配(匈牙利算法)简介& Example hdu 1150 Machine Schedule
二分图匹配(匈牙利算法) 1.一个二分图中的最大匹配数等于这个图中的最小点覆盖数 König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数.如果你还不知 ...
- 【模板】二分图最大匹配(匈牙利算法)/洛谷P3386
题目链接 https://www.luogu.com.cn/problem/P3386 题目大意 给定一个二分图,其左部点的个数为 \(n\),右部点的个数为 \(m\),边数为 \(e\),求其最大 ...
- LightOJ 1356 Prime Independence 二分图最大独立集,HK算法
这个题唯一需要说的就是普通的匈牙利算法是O(nm)的,过不了 然后HK算法可以O(n^0.5m),这个算法可以每次找很多同样长度的最短增广路 分析见:http://www.hardbird.net/l ...
- HDU-1083 Courses 二分图 最大匹配
题目链接:https://cn.vjudge.net/problem/HDU-1083 题意 有一些学生,有一些课程 给出哪些学生可以学哪些课程,每个学生可以选多课,但只能做一个课程的代表 问所有课能 ...
- POJ1469 COURSES 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8232649.html 题目传送门 - POJ1469 题意概括 在一个大矩阵中,有一些障碍点. 现在让你用1*2 ...
随机推荐
- xxtea 文件加密与解密
加密 cocos luacompile -s src -d dst_dir -e -b xxxxx -k xxxxx --disable-compile 解密 cocos luacompile -s ...
- Java IO(一)--File类
File类不是单指文件,它既可以代表一个文件名称,又可以代表一个目录下的一组文件.可以用来创建.删除.遍历文件等 public static void main(String[] args) { St ...
- Laravel Excel安装及最简单使用
官网:https://docs.laravel-excel.com/ 1.安装 1.1.安装要求: PHP: ^7.0 Laravel: ^5.5 PhpSpreadsheet: ^1.6 ...
- php简单实用的调试工具类
<?php /* * 调试类 */ class Common_Debug { //打开错误报告 public static function showError($debug = true) { ...
- MySQL-----一对一
一对一: 用户表和博客表 用户表(userinfo): 用户id 用户名 1 George 2 root 3 Bruce 4 Catherine 博客表: 博客id 博客名 用户id(FK + 唯一) ...
- IP_MULTICAST_LOOP
WINDOWS 中 该选项仅控制接收部分.即设置为0 则控制套接字无法接收自身消息.设置为1 则控制套接字使能接收自身消息. LINUX 中 该先项仅控制发送部分.即设置为0 则控制套 ...
- vs2003 刷新项目失败。无法从服务器中检索文件夹信息
环境: 操作系统:windows server 2003 开发工具:Visual stuadio 2003 FrameWork: 1.1 打开web项目的时候报错 提示 项目刷新失败,无法从服务器 ...
- 【04】AJAX接收服务器返回的数据
AJAX接收服务器返回的数据 readyState 和 status 属性 readyState 属性保存有 XMLHttpRequest 对象的交互状态,从 0 到 4 变化: 0 :未初始化(还没 ...
- .DS_Store的说明
今天清理电脑时,突然发现好像有文件的地方都会出现一个.DS_Store文件,今天有时间,索性就查了一下,并做总结发表一篇吧,怕有什么影响,并未真正实施,仅仅供自己收藏,仅供大家参考. DS_ ...
- Fiddler简介与Web抓包,远程抓包(IE、360、谷歌、火狐)
Fiddler简介以及web抓包 一.Fiddler简介 简单来说,Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯.网上简介很多,我们不多说. 二 ...