POJ - 1469 COURSES (匈牙利算法入门题)
题意:
P门课程,N个学生。给出每门课程的选课学生,求是否可以给每门课程选出一个课代表。课代表必须是选了该课的学生且每个学生只能当一门课程的。
题解:
匈牙利算法的入门题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = ;
int t;
int k, s;
int flag;
int p, n;
int vis[maxn];
int match[maxn];
vector<int> g[maxn];
void init() {
flag = ;
memset(match, -, sizeof(match));
for(int i = ; i <= p+n; i++) g[i].clear();
}
bool dfs(int u) {
vis[u] = ;
for(int i = ; i < g[u].size(); i++) {
int v = g[u][i], w = match[v];
if(w< || !vis[w]&&dfs(w)) {
match[u] = v;
match[v] = u;
return true;
}
}
return false;
}
int hungarian() {
int res = ;
for(int u = ; u <= s+p; u++) {
if(match[u] < ) {
memset(vis, , sizeof(vis));
if(dfs(u)) res++;
}
}
return res;
}
int main() {
scanf("%d", &t);
while(t--) {
init();
scanf("%d%d", &p, &n);
for(int i = ; i <= p; i++) {
scanf("%d", &k);
if(!k) flag = ;
while(k--) {
scanf("%d", &s);
g[i].push_back(s+p);
g[s+p].push_back(i);
}
}
if(flag || n<p) {
puts("NO");
continue;
}
int ans = hungarian();
if(ans == p) puts("YES");
else puts("NO");
}
}
POJ - 1469 COURSES (匈牙利算法入门题)的更多相关文章
- poj 1469 COURSES(匈牙利算法模板)
http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- HDU 1083 - Courses - [匈牙利算法模板题]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1083 Time Limit: 20000/10000 MS (Java/Others) M ...
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
- poj 1274 The Perfect Stall【匈牙利算法模板题】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20874 Accepted: 942 ...
- POJ 1325 && 1274:Machine Schedule 匈牙利算法模板题
Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12976 Accepted: 5529 ...
- POJ 1469 COURSES
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20478 Accepted: 8056 Descript ...
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- POJ 3041.Asteroids-Hungary(匈牙利算法)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23963 Accepted: 12989 Descr ...
- poj 1469 COURSES (二分图模板应用 【*模板】 )
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18454 Accepted: 7275 Descript ...
随机推荐
- Vue 前端md5加密
用户注册时将加密后的密码发送给后端存储 当登陆的时候,再将加密后的密码和数据库中加密的密码相匹配. npm: https://www.npmjs.com/package/crypto-browseri ...
- 屏蔽datatable错误提示
$.fn.dataTable.ext.errMode = 'none'; //不显示任何错误信息// 以下为发生错误时的事件处理,如不处理,可不管.$('#productionRequestItems ...
- MySQL运行一段时间后自动停止问题的排查
在进入主题前,一定要先吐槽下自己,前段时间购买了一台阿里云服务器,最开始打算只是自己个人用的,就买了一台配置很寒碜的服务器: CPU: 1核 内存: 1 GB 操作系统: CentOS 7.2 64位 ...
- 【jQuery】input框输入手机号自动填充空格
<input type="tel" id="tel"> $("#tel").keyup(function(){ _self = ...
- PHP无限分类生成树方法,非递归,引用
//这个是核心方法 function generateTree($items){ $tree = array(); foreach($items as $item){ ...
- Pandas基本命令
关键缩写和包导入 在这个速查手册中,我们使用如下缩写: df:任意的Pandas DataFrame对象 同时我们需要做如下的引入: import pandas as pd 创建测试对象 import ...
- 第一章 UNIX 基础知识
1.1 Unix体系结构 OS定义为一种软件,它控制计算机硬件资源,提供程序运行环境,一般称其为内核(kernel),它体积小,位于环境中心. 内核的接口为系统调用(system call),共用函数 ...
- Wireshark 的使用
Wireshark 默认无法查看 https, 需要设置一下 新建环境变量 SSLKEYLOGFILE, 值为一个想要保存 sshKey 的文件, 如新建一个空文件 D:\AppData\SshKey ...
- PHP代码审计5-实战漏洞挖掘-cms后台登录绕过
cms后台登录绕过 练习源码:[来源:源码下载](数据库配置信息有误,interesting) 注:需进行安装 1.创建数据库 2.设置账号密码,连接数据库 3.1 正常登录后台,抓包分析数据提交位置 ...
- C# Dictionary的遍历理解
C# Dictionary容器类的理解 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/det ...