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> ...
随机推荐
- ASP.NET Core - 实现自定义WebApi模型验证
Framework时代 在Framework时代,我们一般进行参数验证的时候,以下代码是非常常见的 [HttpPost] public async Task<JsonResult> Sav ...
- 深入理解JVM-类加载器深入解析(2)
深入理解JVM-类加载器深入解析(2) 加载:就是把二进制形式的java类型读入java虚拟机中 连接: 验证: 准备:为类变量分配内存,设置默认值.但是在到达初始化之前,类变量都没有初始化为真正的初 ...
- 15分钟让你了解如何实现并发中的Barrier
说到Barrier,很多语言中已经是标准库中自带的概念,一般情况下,只需要直接使用就行了.而最近一些机缘巧合的机会,我需要在c++中使用这么个玩意儿.但是c++标准库里还没有这个概念,只有boost里 ...
- bat 搜索进程名并kill
@echo off set/p "target=进程名(默认nginx): "if not defined target (set "target=nginx" ...
- [NUnit] discover test finished: 0 found issue
%Temp%\VisualStudioTestExplorerExtensions & restart visual studio
- python3学习-Queue模块
python标准库中带有一个Queue模块,顾名思义,队列.该模块也衍生出一些基本队列不具有的功能. 我们先看一下队列的方法: put 存数据 get 取数据 empty 判断队列是否为空 qsize ...
- java并发编程(十五)----(线程池)java线程池简介
好的软件设计不建议手动创建和销毁线程.线程的创建和销毁是非常耗 CPU 和内存的,因为这需要 JVM 和操作系统的参与.64位 JVM 默认线程栈是大小1 MB.这就是为什么说在请求频繁时为每个小的请 ...
- RE最全面的正则表达式----终结篇 特殊处理
三.特殊需求表达式 Email地址:^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0- ...
- hadoop hdfs 分布式存储
1.克隆前的工作 1.配置好网络nat 需要设置静态ip并能通过主机上网 ssh 和 rsync 是必须下载的 2.yum install vim wget rsync ssh 并配 ...
- Oracle Job定时任务详解、跨数据库数据同步
业务需求,需要与A公司做数据对接,我们公司用的Oracle,A公司用的SQL Server数据库,如何跨数据库建立连接呢?这里使用的是DBLink,不会配置的请看我的另外一篇博客:https://ww ...