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

Your program should read sets of data from a text file. The first line of the input file 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'll 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.

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.

An example of program input and output:

Input

2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1

Output

YES
NO

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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAXN 317
int LN,RN;//L,R数目
int g[MAXN][MAXN], linker[MAXN];
bool used[MAXN];
int dfs(int L)//从左边开始找增广路径
{
int R;
for(R = ; R <= RN; R++)
{
if(g[L][R]!= && !used[R])
{
//找增广路,反向
used[R]=true;
if(linker[R] == - || dfs(linker[R]))
{
linker[R]=L;
return ;
}
}
}
return ;//这个不要忘了,经常忘记这句
}
int hungary()
{
int res = ;
int L;
memset(linker,-,sizeof(linker));
for( L = ; L <= LN; L++)
{
memset(used,,sizeof(used));
if(dfs(L) != )
res++;
}
return res;
}
int main()
{
int t;
int p, n;
int k, L, R;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&p,&n);
memset(g,,sizeof(g));
for(int i = ; i <= p; i++)
{
scanf("%d",&k);
for(int j = ; j <= k; j++)
{
scanf("%d",&R);
L = i;
g[L][R] = ;
}
}
LN = p;
RN = n;
int res = hungary();//最大匹配数
//printf("%d\n",res);
if(res == p)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return ;
}

Courses HDU - 1083 (二分匹配模板题)的更多相关文章

  1. (匹配)Courses -- hdu --1083

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1083 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. C - Courses - hdu 1083(模板)

    一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是否达成: 1.每个学生选的都是不同的课(即不能有两个学生选同一门课) 2.每门课都有一个代表(即P门课都被成功选过) 输入为: P N( ...

  3. HDU - 1054 Strategic Game (二分图匹配模板题)

    二分图匹配模板题 #include <bits/stdc++.h> #define FOPI freopen("in.txt", "r", stdi ...

  4. hdu 4024 二分

    转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html   一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...

  5. codeforces 1165F1/F2 二分好题

    Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...

  6. HDU 1083 - Courses - [匈牙利算法模板题]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1083 Time Limit: 20000/10000 MS (Java/Others) M ...

  7. HDU 1083 Courses(二分图匹配模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 题意:有p门课和n个学生,每个学生都选了若干门课,每门课都要找一个同学来表演,且一个同学只能表演一门课,判 ...

  8. HDU 1083 Courses(最大匹配模版题)

    题目大意: 一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是否达成:    1.每个学生选的都是不同的课(即不能有两个学生选同一门课)   2.每门课都有一个代表(即P门课都被成功选过 ...

  9. hdu - 1083 - Courses

    题意:有P门课程,N个学生,每门课程有一些学生选读,每个学生选读一些课程,问能否选出P个学生组成一个委员会,使得每个学生代言一门课程(他必需选读其代言的课程),每门课程都被一个学生代言(1 <= ...

随机推荐

  1. php中经常使用的string函数

    strpos() ---返回字符串在另一字符串中首次出现的位置 strrpos() ---查找字符串在另一字符串中最后出现的位置 strchr()   ===  strstr()    ---找到字符 ...

  2. odoo方法

    一. 类似于前面有个_ 的方法,格式是如下def _getdlvqty(self, cr, uid, ids, field_names, args, context=None): def _get_p ...

  3. C++面向对象模型

    1. 基础知识 C++编译器怎样完毕面向对象理论到计算机程序的转化? 换句话:C++编译器是怎样管理类.对象.类和对象之间的关系 详细的说:详细对象调用类写的方法,那,c++编译器是怎样区分,是那个详 ...

  4. MapReduce -- 最短路径

    示例: 给出各个节点到相邻节点的距离,要求算出初始节点到各个节点的最短路径. 数据: A (B,) (D,) B (C,) (D,) C (E,) D (B,) (C,) (E,) E (A,) (C ...

  5. Ruby知识总结-一般变量+操作符+if+数组和哈希

    ruby入门掌握其实很简单,下面对我司主要使用的部分入门做一个简单的归纳总结: 本文的文章结构: 1.变量 2.操作符 3.if~else~end .unless 4.数组(Array) 5.哈希(H ...

  6. ABC Tech Day(2018.08.11)

    时间:2018.07.24地点:北京中关村创业大街车库咖啡

  7. 大数据入门第二十天——scala入门(一)入门与配置

    一.概述 1.什么是scala  Scala是一种多范式的编程语言,其设计的初衷是要集成面向对象编程和函数式编程的各种特性.Scala运行于Java平台(Java虚拟机),并兼容现有的Java程序. ...

  8. C# 文本转语音朗读

    1. 利用DONET框架自带的 SpeechSynthesizer ,缺点是没有感情色彩,抑扬顿挫等. using System; using System.Collections.Generic; ...

  9. 大白话说Java泛型:入门、使用、原理

    文章首发于[博客园-陈树义],点击跳转到原文<大白话说Java泛型:入门.使用.原理> 远在 JDK 1.4 版本的时候,那时候是没有泛型的概念的.当时 Java 程序员们写集合类的代码都 ...

  10. Visual Studio Package 插件开发(Visual Studio SDK)

    背景 这段时间公司新做了一个支付系统,里面有N个后台服务,每次有更新修改,拷贝打包发布包“不亦乐乎”...于是我想要不要自己定制个打包插件. 部分朋友可能会认为,有现成的可以去找一个,干嘛不用持续集成 ...