COURSES
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18993   Accepted: 7486

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

简单的二分匹配,将学生看成要匹配的集合X,将课程看做要匹配的集合Y。当最大匹配 == 课程数的时候,就输出YES,否则就输出NO。

代码例如以下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 500;
int p, n;
int s[N][N], c[N], used[N]; bool find(int x)
{
for(int j = 1; j <= p; j++)
{
if(s[x][j] == 1 && used[j] == 0)
{
used[j] = 1;
if(c[j] == 0 || find(c[j]))
{
c[j] = x;
return true;
}
}
}
return false;
} int main()
{
int t = 0;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &p, &n);
int x, st;
memset(s, 0, sizeof(s));
memset(c, 0, sizeof(c));
for(int i = 1; i <= p; i++)
{
scanf("%d", &x);
while(x--)
{
scanf("%d", &st);
s[st][i] = 1;
}
}
int all = 0;
for(int i = 1; i <= n; i++)
{
memset(used, 0, sizeof(used));
if(find(i))
all+= 1;
}
if(all == p) printf("YES\n");
else printf("NO\n");
}
return 0;
}

POJ 1469(裸二分匹配)的更多相关文章

  1. POJ 1469 ZOJ1140 二分匹配裸题

    很裸,左点阵n,右点阵m 问最大匹配是否为n #include <cstdio> #include <cstring> #include <vector> usin ...

  2. poj 1469 COURSES (二分匹配)

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16877   Accepted: 6627 Descript ...

  3. POJ 3041 - 最大二分匹配

    这道题实现起来还是比较简单的,但是理解起来可能有点困难. 我最开始想到的是贪心法,每次消灭当前小行星最多的一行或一列.然而WA了.Discuss区里已经有高人给出反例. 下面给出正确的解法 我们把行和 ...

  4. poj 2446 Chessboard (二分匹配)

    Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12800   Accepted: 4000 Descr ...

  5. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  6. POJ 3057 Evacuation (二分匹配)

    题意:给定一个图,然后有几个门,每个人要出去,但是每个门每个秒只能出去一个,然后问你最少时间才能全部出去. 析:初一看,应该是像搜索,但是怎么保证每个人出去的时候都不冲突呢,毕竟每个门每次只能出一个人 ...

  7. POJ 2289 多重二分匹配+二分 模板

    题意:在通讯录中有N个人,每个人能可能属于多个group,现要将这些人分组m组,设各组中的最大人数为max,求出该最小的最大值 下面用的是朴素的查找,核心代码find_path复杂度是VE的,不过据说 ...

  8. POJ 1469 COURSES(二部图匹配)

                                                                     COURSES Time Limit: 1000MS   Memory ...

  9. Guardian of Decency POJ - 2771 【二分匹配,最大独立集】

    Problem DescriptionFrank N. Stein is a very conservative high-school teacher. He wants to take some ...

随机推荐

  1. 万能日志数据收集器 Fluentd - 每天5分钟玩转 Docker 容器技术(91)

    前面的 ELK 中我们是用 Filebeat 收集 Docker 容器的日志,利用的是 Docker 默认的 logging driver json-file,本节我们将使用 fluentd 来收集容 ...

  2. Tinyhttpd精读解析

    首先,本人刚刚开始开源代码精读,写的不对的地方,大家轻拍,一起进步.本文是对Tinyhttpd的一次精读,大家每天都在用着http服务,很多人也一直活跃在上层,使用IIS.Apache等,大家是否想看 ...

  3. 问题:编译eshoponcontainers失败,提示error:invalid reference format

    环境: visual studio 2017 v15.4.2,docker ce Version 17.06.0-ce-win19 (12801) 参考问题页: https://github.com/ ...

  4. 应用在安卓和ios端APP的证件识别

    移动端证件识别智能图文处理,是利用OCR识别技术,通过手机拍摄身份证图像或者从手机相册中加载证件图像,过滤身份证的背景底纹干扰,自动分析证件各文字进行字符切分.识别,最后将识别结果按姓名.地址.民族. ...

  5. 线程中WICImage与Bitmap数据转换

    最近项目开发, 要用到线程中对图像进行缩放和二值化处理 为了省事, 图像缩放用的WICImage.ImagingFactory接口, 二值化用的是bitmap.PixelFormat := pf1bi ...

  6. 利用Pycharm本地调试spark-streaming(包含kafka和zookeeper等操作)

    环境准备就不说了! 第一步:打开Pycharm,在File->Setting->Project Structure中点击Add Content Root 添加本地python调用java和 ...

  7. [转载] 红黑树(Red Black Tree)- 对于 JDK TreeMap的实现

    转载自http://blog.csdn.net/yangjun2/article/details/6542321 介绍另一种平衡二叉树:红黑树(Red Black Tree),红黑树由Rudolf B ...

  8. CLR之委托的揭秘(二)

    杂谈 在开始真正的代码之前,分析一下上周的一些工作内容,发现自己在代码上还是有很多小毛病需要纠正和去更改的,首先之前一直疏于文档的整理,几乎很少去写文档,第二对于接口开发过程中缺少一定的严谨性,很多问 ...

  9. [转]分布式消息中间件 MetaQ 作者庄晓丹专访

    MetaQ(全称Metamorphosis)是一个高性能.高可用.可扩展的分布式消息中间件,思路起源于LinkedIn的Kafka,但并不是Kafka的一个Copy.MetaQ具有消息存储顺序写.吞吐 ...

  10. CVE-2017-11882漏洞利用

    CVE-2017-11882漏洞利用 最新Office的CVE-2017-11882的poc刚刚发布出来,让人眼前一亮,完美无弹窗,无视宏,影响Ms offcie全版本,对于企业来说危害很大.在此简单 ...