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. 再学UML-Bug管理系统UML2.0建模实例(二)

    2.3 BMS顺序图(需求模型)       在UML中,我们将顺序图分为两类,一类用于描述系统需求,构造系统的需求模型(分析模型):另一类用于指导设计与实现,构造系统的实现模型(设计模型).     ...

  2. 团队合作之Scrum

    CCSU小助手 一:开发团队简介 队名:瓜队 组员:钟文兴.周畅.吉刘磊.唐仲勋 宣言:We are a team at any time! 团队项目描述: 内容:“生活在长大”: 目标:为了方便对学 ...

  3. 如何让一个简单的maven项目支持one-jar 做成一个jar fatjar

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  4. Android(java)学习笔记15:匿名内部类实现多线程

    1. 使用匿名内部类实现多线程 二话不说,首先利用代码体现出来,给大家直观的感觉: package cn.itcast_11; /* 4 * 匿名内部类的格式: 5 * new 类名或者接口名() { ...

  5. Node.js使用MySQL数据库中对RowDataPacket对象的使用

    使用Node.js开发使用MySQL数据库的网站,在查询后返回一RowDataPacket类型的对象 原先使用toString()方法一直得到仅为object的字符串,无法使用 后思考,才发现忽略了其 ...

  6. 【转】学习jar命令 创建和解压jar文件包

    java编程中每天都用不少jar文件,项目开发中不停地导包,在忙完了一天的工作,放下那些复杂的业务实现,不仅想问这些jar包怎么生成的,jar包有哪些独特的地方等等. 原来这些经常见到的jar包是ja ...

  7. 根据ip确定城市

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. 【洛谷P1978】 集合

    集合 题目链接 显然,我们是要把数据先排序的, 然后从大到小枚举每个数,看是否能选上, 能选就选,不能拉倒 若能,二分查找a[i]/k,若查找成功,ans++ 将a[i]/k标记为不能选择 最后输出答 ...

  9. 【洛谷P1351】[NOIP2014]联合权值

    联合权值 题目链接 首先,直接两重循环暴力枚举得了70分 然后发现第二重循环可以记忆化一下 记忆一下每个点的子节点的权值和.最大值. 次大值(为了处理该点的父节点权值恰好为最大值) 具体看代码 #in ...

  10. 188. Best Time to Buy and Sell Stock IV——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...