描述

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 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?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.

样例输入

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

样例输出

YES
NO

题意

有n个学生和p门课,每门课有对应的学生要求,判断能否选出p个学生刚好上p门课

题解

一道很裸的二分图匹配题,刚好拿来熟悉下算法

这里用匈牙利算法,判断p门课程是否都能成功匹配

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const int N=305,P=105;
 5 int G[P][N],vis[N],match[P];
 6 int n,p;
 7 int Find(int u)
 8 {
 9     for(int i=1;i<=n;i++)
10     {
11         if(G[u][i]&&!vis[i])
12         {
13             vis[i]=1;
14             if(!match[i]||Find(match[i]))
15             {
16                 match[i]=u;
17                 return 1;
18             }
19         }
20     }
21     return 0;
22 }
23 int main()
24 {
25     int k,t,stu;
26     cin>>t;
27     while(t--)
28     {
29         memset(G,0,sizeof(G));
30         cin>>p>>n;
31         for(int i=1;i<=p;i++)
32         {
33             cin>>k;
34             for(int j=0;j<k;j++)
35             {
36                 cin>>stu;
37                 G[i][stu]=1;
38             }
39         }
40         int flag=1;
41         memset(match,0,sizeof(match));
42         for(int i=1;i<=p;i++)
43         {
44             memset(vis,0,sizeof(vis));
45             if(!Find(i))
46             {
47                 flag=0;break;
48             }
49         }
50         printf("%s\n",flag?"YES":"NO");
51     }
52 }

转载于:https://www.cnblogs.com/taozi1115402474/p/8724297.html

TZOJ 3030 Courses(二分图匹配)相关推荐

  1. (Hopcroft-Carp二分图匹配)Rain on your Parade

    http://acm.hdu.edu.cn/showproblem.php?pid=2389 经典求最大匹配题,数据范围过大,匈牙利算法超时 #include <stdio.h> #inc ...

  2. Dinic二分图匹配 || Luogu P3386

    题面:[模板]二分图匹配 思路:Dinic实现二分图匹配,要建一个超级源点(S)和超级汇点(T),分别定为N+M+1和N+M+2 然后S去和N中的数建正边和反边,正边权值为1,反边权值为0:M中的数去 ...

  3. 二分图匹配匈牙利算法DFS实现

    1 /*==================================================*\ 2 | 二分图匹配(匈牙利算法DFS 实现) 3 | INIT: g[][]邻接矩阵; ...

  4. poj1274(二分图匹配)

    (一道基础的二分图匹配) 题目意思大概为N个牛和M个栅栏,一个牛和一个栅栏只能匹配一次,求最大匹配 直接套用二分图最大匹配模板即可 #include <iostream> #include ...

  5. poj2724(二分图匹配)

    题目大概意思为有部分奶酪需要处理,若两个奶酪的二进制只有一位不同,则可以一起处理,问最少需要处理几次 题目思路: 将可以一起处理的两个奶酪用边连接在一起,相当于边只连接二进制中有偶数个1的奶酪和二进制 ...

  6. 算法模板——二分图匹配

    实现功能为二分图匹配 原理:匈牙利算法,核心思想--匹配上了就配,没直接匹配上也要通过前面的腾出位置让这个匹配上(详见:趣写算法系列之--匈牙利算法) 本程序以Codevs2776为例 详见Codev ...

  7. 算法:ACM二分图匹配 HDU2063

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 摘录于互联网,原创作者redraiment,很详细的二分图匹配入门资料! 2063 过山车 Pr ...

  8. BZOJ1433 [ZJOI2009]假期的宿舍 - 二分图匹配

    题解 一道裸的二分图匹配,稍微有一点细节需要注意, 但是非常裸= = 本校并且住校的向自己的床和 自己认识的并且本校的人的床连边 离校的不需要向外连边 不是本校的就和自己认识的 并且是本校的人的床连边 ...

  9. POJ2536 二分图匹配

    题意:      有n只老鼠,m个洞,每个洞最多可以藏一只老鼠,每个老鼠的移动速度都是v,给你他们的当前坐标,和洞的坐标,突然老鹰来了,他们必须在s秒内跑到一个洞藏起来,问你最少有多少只老鼠被抓走了. ...

最新文章

  1. “大厂平均年龄 29.6 岁”:不是我们选择了年龄焦虑,是大厂焦虑了我们
  2. shell 函数返回值接收问题
  3. ListView的使用和数据绑定
  4. 前端学习(1173):两种字符串方法
  5. SpringCloud Alibaba Sentinel断路器介绍与控制台搭建
  6. VB.NET 按键代码 及组合键
  7. stream常用操作
  8. linux系统工程师的前途在哪里
  9. VM虚拟机装Windows2000成功后VM Tools失败解决方法
  10. 测试用例经典练习之微信发红包测试用例
  11. 金蝶商贸版,后台修改序时簿过滤条件
  12. 开发集成云信IM小程序聊天室流程
  13. amc 美国数学竞赛能用计算机吗,关于AMC美国数学竞赛的QA
  14. 本地计算机策略打不开怎么办,我的电脑的本地组策略编辑器怎么打不开?
  15. iOS 数据库升级策略
  16. GO语言数据结构之队列
  17. vue学习笔记 DAY1 P1~P6
  18. arp断网攻击解决办法
  19. ubuntu命令行 播放音乐
  20. 视觉检测在激光打码字符检测上的检测案例

热门文章

  1. java mysql dao_Java DAO 模式
  2. behavior php,behavior.php
  3. 游戏开发需要具备哪些技术_短视频 SDK 开发 (一) 开发一款短视频 SDK 需要具备哪些知识?...
  4. 电脑cmd命令大全_电脑定时关机的方法(2)
  5. POJ 3164 Command Network
  6. POJ - 3278 Catch That Cow 简单搜索
  7. 大学每天打游戏,不是混吃等死是什么?
  8. 【Ubuntu-Opencv】Ubuntu14.04 Opencv3.3.0 完整卸载方案
  9. IOS7实现扫描二维码
  10. python3(一)数字Number