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. Angular-cli 构建应用的一些配置

    Angular-cli 构建应用 的一些配置 标签(空格分隔): Angular 直接使用 ng build --prod --build-optimizer --base-href=/ 来发布 ba ...

  2. Asio基本接口

    Asio是C++的网络库,有boost和非boost这两种版本,这里涉及的都是非boost的版本.Asio官方文档 在使用Asio时可以只包含头文件asio.hpp,如果知道所用接口具体在哪个头文件中 ...

  3. Jquery的部分插件

    jQuery Easing:jQuery 动画效果扩展 jQuery Migrate:jQuery 应用迁移辅助插件Modernizr:专为HTML5和CSS3开发的功能检测类库jQuery Flex ...

  4. SQA和测试规程

    SQA *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; ...

  5. TOEFL考试(一年半的复仇,裸考)

    8/11/2018 had a TOEFL test without preparation. Reading (worry about too much, not familiar with the ...

  6. CRUD全栈式编程架构之服务层的设计

    服务层代码 首先我先放出2个主要类的代码再分别讲解 接口 using System; using System.Collections.Generic; using System.Linq; usin ...

  7. [转]Ubuntu桌面入门指南

    Ubuntu桌面入门指南 http://wiki.ubuntu.org.cn/Ubuntu%E6%A1%8C%E9%9D%A2%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97

  8. js通过codeURL画二维码

    一.函数封装 //生成微信二维码 function xyqrcode(options) { var settings = { dom:'', render: 'canvas', //生成二维码的格式还 ...

  9. 使用vba doc转docx

    创建vbs文件,doctodocx.vbs内容如下: '创建一个word对象 set wApp=CreateObject("word.Application") '获取文件传递到参 ...

  10. RL 编、解码(EncodedString、DecodedString) - iOS

    开发中对文本传输或二进制传输,都需要将传输的对象进行二进制字节的转化操作,所以无异于编.解码便会经常用到的操作; 当然除了这种方式之外,还有一种常用的 Base64,此文中不具体细谈, Base64 ...