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. 拓扑排序 HDU - 5695

    众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到NN,在排好队之 ...

  2. 算法:JavaScript两数之和

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  3. 用笔记本写C程序

    首先在Visual Studio Tools 文件中打开 Visual Studio 命令提示(2010),输入命令 notepad 打开记事本,写好程序保存,保存类型:所有文档  文件名:1.c . ...

  4. C#中split分隔字符串的应用

    .用单个字符来分隔:string str="aaajbbbjccc";string[] sArray=str.Split('j');foreach(string i in sArr ...

  5. Angular 5和ASP.NET Core入门

    我希望你们都知道Angular 5已经发布了.在本文中,我们将看到如何使用Angular5TemplateCore开始使用Angular 5和ASP.NET Core. 使用Angular5Templ ...

  6. Python——网络爬虫

    此篇文章继续跟着小甲鱼的视频来初学网络爬虫,除了小甲鱼的网站上可下载视频,发现b站上也有全套的视频哦,会比下载来的更方便些. 网络爬虫,又称为网页蜘蛛(WebSpider),非常形象的一个名字.如果你 ...

  7. [转载] ZooKeeper原理及使用

    转载自http://www.wuzesheng.com/?p=2609 ZooKeeper是Hadoop Ecosystem中非常重要的组件,它的主要功能是为分布式系统提供一致性协调(Coordina ...

  8. Python之多进程篇

    Process 创建子进程执行指定的函数 >>> from multiprocessing import Process,current_process >>> & ...

  9. 实战Excel Add-in的三种玩法

    作者:陈希章 发表于 2017年11月26日 前言 这个系列文章应该有一阵子没有更新了,原因是一如既往的多,但是根本所在是我对于某些章节其实还没有完全想好怎么写,尤其是对于Office Add-in这 ...

  10. 《java.util.concurrent 包源码阅读》21 CyclicBarrier和CountDownLatch

    CyclicBarrier是一个用于线程同步的辅助类,它允许一组线程等待彼此,直到所有线程都到达集合点,然后执行某个设定的任务. 现实中有个很好的例子来形容:几个人约定了某个地方集中,然后一起出发去旅 ...