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. npm run dev运行Vue项目报错:Node Sass does not yet support your current environment

    导入Vue项目后,#npm run dev 报错: error in ./src/pages/hello.vue Module build failed: Error: Node Sass does ...

  2. C#实现文件异步上传

    //前台方法,包含弹出框确认以及文件选择<input type="button" id="importxlsx" name="importxls ...

  3. WAKE-WIN10-SOFT-GITHUB

    1,GITHUB 官网:https://github.com/ 2,软件工具 ,,,,,,

  4. bzoj1965 [Ahoi2005]洗牌

    Description 为了表彰小联为Samuel星球的探险所做出的贡献,小联被邀请参加Samuel星球近距离载人探险活动. 由于Samuel星球相当遥远,科学家们要在飞船中度过相当长的一段时间,小联 ...

  5. bzoj3816 矩阵变换

    Description 给出一个 N 行 M 列的矩阵A, 保证满足以下性质: M>N. 矩阵中每个数都是 [0,N] 中的自然数. 每行中, [1,N] 中每个自然数都恰好出现一次.这意味着每 ...

  6. Sublime Text 插件 【转】

    好厉害,好漂亮. http://www.cnsecer.com/460.html 安装Sublime Text 3插件的方法: 朋友们,小站活着不容易,全靠广告费养着了,如果本文对你有帮助.麻烦动下手 ...

  7. luogu P1121 环状最大两段子段和

    嘟嘟嘟 一道说难也难说简单也简单的dp题. 我觉得我的(有篇题解)做法就属于特别简单的. 平时遇到环的问题都是断环为链,但这道题给了一种新的思路. 观察一下,最后的答案无非就这两种:xxx--xx-- ...

  8. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains 【树状数组维护区间最大值】

    题目传送门:http://codeforces.com/contest/799/problem/C C. Fountains time limit per test 2 seconds memory ...

  9. [18/11/28]Java中的包(package)

    一.为何引入包? 包机制是Java中管理类的重要手段. 开发中,我们会遇到大量同名的类,通过包我们很容易对解决类重名的问题,也可以实现对类的有效管理. 包对于类,相当于文件夹对于文件的作用.(同一文件 ...

  10. ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(六)之 好友申请、同意、拒绝

    不知道距离上一篇多久没有写了,可能是因为忙(lan)的关系吧.废话不多说,今天要介绍的不算什么新知识,主要是逻辑上的一些东西.什么逻辑呢,加好友,发送好友申请,对方审批通过,拒绝.(很遗憾,对方审批通 ...