POJ-1469 COURSES ( 匈牙利算法 dfs + bfs )
题目链接: http://poj.org/problem?id=1469
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
program should read sets of data from the std input. The first line of
the input contains the number of the data sets. Each data set is
presented in the following format:
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
result of the program is on the standard output. For each input data set
the program prints on a single line "YES" if it is possible to form a
committee and "NO" otherwise. There should not be any leading blanks at
the start of the line.
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 模板题,使用匈利亚算法找最大匹配,匈利亚算法有dfs和bfs两种实现,dfs代码短,容易理解,bfs有点复杂,据说速度要快一点,我两种都写了一下。
//dfs
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; int p, n;
bool visit[][];
int stop[];
bool used[]; bool search( int x ){
for( int i = ; i <= n; i++ )
if( visit[x][i] && !used[i] ){
used[i] = true;
if( stop[i] == || search( stop[i] ) ){
stop[i] = x;
return true;
}
} return false;
} int main(){
int T;
scanf( "%d", &T );
while( T-- ){
memset( visit, false, sizeof( visit ) );
memset( stop, , sizeof( stop ) );
scanf( "%d%d", &p, &n );
int flag = true; for( int i = ; i <= p; i++ ){
int temp, a;
scanf( "%d", &temp );
while( temp-- ){
scanf( "%d", &a );
visit[i][a] = true;
}
} for( int i = ; i <= p; i++ ){
if( !flag ) continue;
else{
memset( used, false, sizeof( used ) );
if( !search( i ) ){
flag = false;
}
}
} if( flag ) printf( "YES\n" );
else printf( "NO\n" );
} return ;
}
//bfs
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std; int n, p;
bool visit[][];
int ptos[], stop[], pre[];//ptos,stop记录匹配,pre记录课程匹配时的前驱点
int flag[];//寻找增广路时判断是否已经添加过 int maxmatch(){
int ans = ;
memset( ptos, , sizeof( ptos ) );
memset( stop, , sizeof( stop ) );
memset( flag, false, sizeof( flag ) ); for( int i = ; i <= p; i++ ){ if( ptos[i] == ){//没有匹配
queue<int> Q;
Q.push( i );
pre[i] = ;
bool ok = false;//判断是否找到增广路 while( !Q.empty() && !ok ){//没有找到增广路
int u = Q.front();
Q.pop(); for( int k = ; k <= n && !ok; k++ ){//开始寻找增广路
if( visit[u][k] && flag[k] != i ){
flag[k] = i;
Q.push( stop[k] );
if( stop[k] == ){
ok = true;
int tempp = u,temps = k;
while( tempp != ){//改变匹配
int x = ptos[tempp];
ptos[tempp] = temps;
stop[temps] = tempp;
tempp = pre[tempp];
temps = x;
}
}
else pre[stop[k]] = u;
}
}
}
if( ptos[i] != ) ans++;
}
} return ans;
} int main(){
int T;
scanf( "%d", &T );
while( T-- ){
memset( visit, false, sizeof( visit ) );
scanf( "%d%d", &p, &n ); int temp, a;
for( int i = ; i <= p; i++ ){
scanf( "%d", &temp );
while( temp-- ){
scanf( "%d", &a );
visit[i][a] = true;
}
} if( p == maxmatch() ) printf( "YES\n" );
else printf( "NO\n" );
} return ;
}
POJ-1469 COURSES ( 匈牙利算法 dfs + bfs )的更多相关文章
- poj 1469 COURSES(匈牙利算法模板)
http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
- POJ - 1469 COURSES (匈牙利算法入门题)
题意: P门课程,N个学生.给出每门课程的选课学生,求是否可以给每门课程选出一个课代表.课代表必须是选了该课的学生且每个学生只能当一门课程的. 题解: 匈牙利算法的入门题. #include < ...
- POJ 1469 COURSES
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20478 Accepted: 8056 Descript ...
- 匈牙利算法dfs模板 [二分图][二分图最大匹配]
最近学了二分图最大匹配,bfs模板却死活打不出来?我可能学了假的bfs 于是用到了dfs模板 寻找二分图最大匹配的算法是匈牙利算法 匈牙利算法的主要程序是寻找增广路 寻找增光路是过程是:从一个未经配对 ...
- poj 1469 COURSES (二分图模板应用 【*模板】 )
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18454 Accepted: 7275 Descript ...
- POJ 2771 最大独立集 匈牙利算法
(为什么最大独立集的背景都是严打搞对象的( _ _)ノ|壁) 思路:匈牙利算法 没什么可说的-- // by SiriusRen #include <cstdio> #include &l ...
- POJ 3041.Asteroids-Hungary(匈牙利算法)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23963 Accepted: 12989 Descr ...
- POJ 1469 COURSES 二分图最大匹配 二分图
http://poj.org/problem?id=1469 这道题我绝壁写过但是以前没有mark过二分图最大匹配的代码mark一下. 匈牙利 O(mn) #include<cstdio> ...
随机推荐
- Eclipse "R cannot be resolved"问题
前两天 Eclipse 又遇到了这个问题.网上找了不少,不过最终还是没能解决我的问题,无奈重装了 Eclipse…… 搜索中找到了下面这几篇文章,常见的解决方法都在这里,还是不错的,分享一下: htt ...
- Kafka服务不可用(宕机)问题踩坑记
背景 某线上日志收集服务报警,打开域名报502错误码. 收集服务由2台netty HA服务器组成,netty服务器将客户端投递来的protobuf日志解析并发送到kafka,打开其中一个应用的日志,发 ...
- 设置Myeclipse的jvm内存参数
Myeclipse经常会遇到内存溢出和Gc开销过大的情况,这时候就需要修改Myeclipse的Jvm内存参数 修改如下:(使用Extjs做公司大项目时候,不要让项目Builders的Javascrip ...
- 佳木斯集训Day8
本来能AK的啊啊啊啊啊,唯一一天可以AK,却被Champion误导了(好吧实际上是我理解有问题) T1我写了俩小时,就是一道数列题,推公式的,可以二分解,我觉得二分麻烦,就直接想O(1)了 #incl ...
- javascript基础案例解析
学完了JavaScript基础部分,总结出一些基本案例,以备日后查看! 1.九九乘法口诀表:在控制台中输出九九乘法口诀表!代码如下: <!DOCTYPE html> <html> ...
- 转载 | embed用法(网站中视频、音频的添加)
网站中添加视频: <embed src="http://player.video.qiyi.com/390cf6c74450e4c70b7bd2d883169914/0/0/w_19r ...
- 全球十大OTA 谁能有一席之地?
全球十大OTA 谁能有一席之地? http://www.traveldaily.cn/article/78381/1 2014-03-05 来源:i黑马 随着旅游行业日新月异的发展,在线旅游网站的出现 ...
- JavaScript数据结构——图的实现
在计算机科学中,图是一种网络结构的抽象模型,它是一组由边连接的顶点组成.一个图G = (V, E)由以下元素组成: V:一组顶点 E:一组边,连接V中的顶点 下图表示了一个图的结构: 在介绍如何用Ja ...
- 重读《学习JavaScript数据结构与算法-第三版》-第2章 ECMAScript与TypeScript概述
定场诗 八月中秋白露,路上行人凄凉: 小桥流水桂花香,日夜千思万想. 心中不得宁静,清早览罢文章, 十年寒苦在书房,方显才高志广. 前言 洛伊安妮·格罗纳女士所著的<学习JavaScript数据 ...
- 【原创】微信小程序支付java后台案例(公众号支付同适用)(签名错误问题)
前言 1.微信小程序支付官方接口文档:[点击查看微信开放平台api开发文档]2.遇到的坑:预支付统一下单签名结果返回[签名错误]失败,建议用官方[签名验证工具]检查签名是否存在问题.3.遇到的坑:签名 ...