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

Source

题解:

标准的匈牙利算法,最需要最后的匹配树等于课程数,我感觉最大流也是可以写的。

#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN=50005;
struct node{
int v,next;
}edge[MAXN];
int cnt=0,head[MAXN];
int p,n;
void add(int x,int y)
{
edge[cnt].v=y;
edge[cnt].next=head[x];
head[x]=cnt++;
}
int ans=0;
bool vis[MAXN];
int match[MAXN];
bool findpath(int x)
{
for (int i = head[x]; i !=-1 ; i=edge[i].next) {
int v=edge[i].v;
if(!vis[v])
{
vis[v]=true;
if(match[v]==-1||findpath(match[v]))
{
match[v]=x;
return true;
}
}
}
return false;
}
void hungry()
{
for (int i = 1; i <=p; ++i) {
memset(vis,false, sizeof(vis));//没次都去初始化
if(findpath(i))//寻找是否有增广路
ans++;
}
}
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
cnt=0;
ans=0;
memset(head,-1, sizeof(head));
memset(match,-1, sizeof(match));
memset(vis,false, sizeof(vis));
scanf("%d%d",&p,&n);
int num,x;
for (int i = 1; i <=p ; ++i) {
scanf("%d",&num);
while(num--)
{
scanf("%d",&x);
add(i,x);
}
}
hungry();
if(ans==p)
printf("YES\n");
else
puts("NO");
}
return 0;
}

  

COURSES POJ1469(模板)的更多相关文章

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

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

  2. 《Django By Example》第十章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译本章过程中几次想放弃,但是既然 ...

  3. POJ-1469 COURSES ( 匈牙利算法 dfs + bfs )

    题目链接: http://poj.org/problem?id=1469 Description Consider a group of N students and P courses. Each ...

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

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

  5. POJ1469 COURSES 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8232649.html 题目传送门 - POJ1469 题意概括 在一个大矩阵中,有一些障碍点. 现在让你用1*2 ...

  6. HDU 1083 - Courses - [匈牙利算法模板题]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1083 Time Limit: 20000/10000 MS (Java/Others) M ...

  7. POJ1469 COURSES 【二分图最大匹配&#183;HK算法】

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17777   Accepted: 7007 Descript ...

  8. F - Courses (学生选课(匈牙利算法模板))

    题目大意:一共有N个学生跟P门课程,一个学生可以任意选一门或多门课,问是否达成: 1.每个学生选的都是不同的课(即不能有两个学生选同一门课) 2.每门课都有一个代表(即P门课都被成功选过) 输入为: ...

  9. poj 2239 Selecting Courses(二分匹配简单模板)

    http://poj.org/problem?id=2239 这里要处理的是构图问题p (1 <= p <= 7), q (1 <= q <= 12)分别表示第i门课在一周的第 ...

随机推荐

  1. FIFO认识(一)

    1.什么是FIFO? FIFO是英文First In First Out 的缩写,是一种先进先出的数据缓存器,他与普通存储器的区别是没有外部读写地址线,这样使用起来非常简单,但缺点就是只能顺序写入数据 ...

  2. (EXPDP) Fails With Errors ORA-39079 ORA-25306 On One Node In RAC Environment

    分类: Oracle DataPump export on one certain RAC instance fails with errors: ORA-39006: internal errorO ...

  3. WIN10安装VS2013出现兼容性问题解决

    在WIN10安装VS2013时,会提示“windows程序兼容模式已打开”,通过搜索引擎搜索的常见方案为: 1.使用命令行安装,进入vs_ultimate文件所在目录,输入:vs_ultimate / ...

  4. Linux获取系统当前时间(精确到毫秒)

    #include <stdio.h> #include <time.h> #include <sys/time.h> void sysLocalTime() { t ...

  5. 真的有用吗?(GitHub)

    为什么要新建一个GitHub账号   一个程序员不知道GitHub,那我就笑笑,呵呵哒.什么是GitHub呢? 就我知道的git,谈一下.Git是一个版本控制软件,这个软件最初是Linux之父林纳斯. ...

  6. 使用字面量或者绑定变量在HANA Studio里执行SQL语句

    在SAP note 2000002 – FAQ: SAP HANA SQL Optimization里提到了SQL语句的两种执行方式,具体差异体现在where语句里搜索条件的指定方式上. 所谓Lite ...

  7. 未启用当前数据库的 SQL Server Service Broker,请为此数据库启用 Service Broker

    ALTER DATABASE DATABASE_Name SET NEW_BROKER WITH ROLLBACK IMMEDIATE; ALTER DATABASE DATABASE_Name SE ...

  8. 深度优先搜索(深搜)——Deep First Search【例题:迷宫】

           深度优先搜索 基本思想:先选择一种可能情况向前探索,在探索过程中,一点那发现原来的选择是错误的,就退回一步重新选择,继续向前探索,(回溯)反复进行. [例题]迷宫问题           ...

  9. 今天升级win10.vs调试程序各种崩溃

    今天升级win10.vs调试程序各种崩溃.感觉代码没问题.崩溃时有时没有.不知道是win10的问题,好真是我的代码问题. 问题1: 尝试读取或写入受保护的内存.这通常指示其他内存已损坏 不过.当我写这 ...

  10. php如何实现登陆后返回原页面

    访问网站页面时,有的页面需要授权才能访问,这时候就会要求用户登录,跳转到登录页面login.php,怎么实现登录后返回到刚才访问的页面项目需求 访问网站页面时,有的页面需要授权才能访问,这时候就会要求 ...