题目链接: http://poj.org/problem?id=1469

Description

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:

  • 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

Your
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

The
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 )的更多相关文章

  1. poj 1469 COURSES(匈牙利算法模板)

    http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  2. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  3. POJ - 1469 COURSES (匈牙利算法入门题)

    题意: P门课程,N个学生.给出每门课程的选课学生,求是否可以给每门课程选出一个课代表.课代表必须是选了该课的学生且每个学生只能当一门课程的. 题解: 匈牙利算法的入门题. #include < ...

  4. POJ 1469 COURSES

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20478   Accepted: 8056 Descript ...

  5. 匈牙利算法dfs模板 [二分图][二分图最大匹配]

    最近学了二分图最大匹配,bfs模板却死活打不出来?我可能学了假的bfs 于是用到了dfs模板 寻找二分图最大匹配的算法是匈牙利算法 匈牙利算法的主要程序是寻找增广路 寻找增光路是过程是:从一个未经配对 ...

  6. poj 1469 COURSES (二分图模板应用 【*模板】 )

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18454   Accepted: 7275 Descript ...

  7. POJ 2771 最大独立集 匈牙利算法

    (为什么最大独立集的背景都是严打搞对象的( _ _)ノ|壁) 思路:匈牙利算法 没什么可说的-- // by SiriusRen #include <cstdio> #include &l ...

  8. POJ 3041.Asteroids-Hungary(匈牙利算法)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23963   Accepted: 12989 Descr ...

  9. POJ 1469 COURSES 二分图最大匹配 二分图

    http://poj.org/problem?id=1469 这道题我绝壁写过但是以前没有mark过二分图最大匹配的代码mark一下. 匈牙利 O(mn) #include<cstdio> ...

随机推荐

  1. Git 从master拉取代码创建新分支

    从master拉取新分支并push到远端 开发过程中经常用到从master分支copy一个开发分支: 1.切换到被copy的分支(master),并且从远端拉取最新版本 $git checkout m ...

  2. 小白学python-day08-文件及其操作、字符串字典类型转换

    今天是day08,以下是学习总结: 但行努力,莫问前程. ----------------------------------------------------------------------- ...

  3. OI/ACM最全卡常大招

    NO.10: 循环展开: 在缓存和寄存器允许的情况下一条语句内大量的展开运算会刺激 CPU 并发(蛤?这是个什么原理,算了,反正写了没坏处就这么写吧) NO.9: 特殊运算优化:(或许这真的没用) 取 ...

  4. 给最近正在找工作(iOS)的朋友一些建议/经验

    众所周知, iOS开发找工作越来越难, 企业要求越来越高,一方面是资本寒冬期+七八月是企业招人淡季, 另外一方面也是iOS市场饱和.最近有出去看新机会, 所以下面记录一下面试XimalayaFM的大概 ...

  5. 【POJ - 2385】Apple Catching(动态规划)

    Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...

  6. linuxdeploy安装报错

    报错内容:checking installation path…fail(检查安装路径) 处理方法:安装在手机自带的存储空间中,则在路径开头加上${ENV_DIR},安装在sdcard中,加上${EX ...

  7. springboot自动配置源码解析

    springboot版本:2.1.6.RELEASE SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfig ...

  8. c#Winform自定义控件-目录

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. zabbix自发现item监控

    在zabbix监控中,我们可以通过自带item的可以和自定义key进行监控,但是当所需要的监控项不确定,比如key会根据时间进行变化时,这时候我们就不能把item的key定义死,要通过自发现这个高级功 ...

  10. JVM(十二):方法调用

    JVM(十二):方法调用 在 JVM(七):JVM内存结构 中,我们说到了方法执行在何种内存结构上执行:Java 方法活动在虚拟机栈中的栈帧上,栈帧的具体结构在内存结构中已经详细讲解过了,下面就让我们 ...