描述

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门课程是否都能成功匹配

代码

 #include<bits/stdc++.h>
using namespace std; const int N=,P=;
int G[P][N],vis[N],match[P];
int n,p;
int Find(int u)
{
for(int i=;i<=n;i++)
{
if(G[u][i]&&!vis[i])
{
vis[i]=;
if(!match[i]||Find(match[i]))
{
match[i]=u;
return ;
}
}
}
return ;
}
int main()
{
int k,t,stu;
cin>>t;
while(t--)
{
memset(G,,sizeof(G));
cin>>p>>n;
for(int i=;i<=p;i++)
{
cin>>k;
for(int j=;j<k;j++)
{
cin>>stu;
G[i][stu]=;
}
}
int flag=;
memset(match,,sizeof(match));
for(int i=;i<=p;i++)
{
memset(vis,,sizeof(vis));
if(!Find(i))
{
flag=;break;
}
}
printf("%s\n",flag?"YES":"NO");
}
}

TZOJ 3030 Courses(二分图匹配)的更多相关文章

  1. POJ1469 COURSES 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8232649.html 题目传送门 - POJ1469 题意概括 在一个大矩阵中,有一些障碍点. 现在让你用1*2 ...

  2. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  3. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  4. POJ 1274 裸二分图匹配

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

  5. BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2375  Solved: 1005[Submit][Sta ...

  6. HDU1281-棋盘游戏-二分图匹配

    先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...

  7. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

  8. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  9. BZOJ 1059 & 二分图匹配

    题意: 判断一个黑白染色的棋盘能否通过交换行或列使对角线上都是黑色. SOL: 真是有点醉...这种问题要么很神要么很水...第一眼感觉很水但就是不造怎么做...想了10分钟怎么感觉就是判断个数够不够 ...

随机推荐

  1. crontab使用说明及例子程序

    http://blog.csdn.net/yygydjkthh/article/details/7845639 http://walkerqt.blog.51cto.com/1310630/16901 ...

  2. 关于cordova 状态栏设置

    https://blog.csdn.net/u011127019/article/details/58104056

  3. zookeeper和dubbo的关系

    Dubbo建议使用Zookeeper作为服务的注册中心. 1.   Zookeeper的作用:         zookeeper用来注册服务和进行负载均衡,哪一个服务由哪一个机器来提供必需让调用者知 ...

  4. B树、B-树、B+树、B*树的定义和区分

    MySQL是基于B+树聚集索引组织表 B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右 ...

  5. C# 递归获取 文件夹的 所有文件

    public void Director(string dir, List<string> list) { DirectoryInfo d = new DirectoryInfo(dir) ...

  6. 吴裕雄 python 机器学习-KNN算法(1)

    import numpy as np import operator as op from os import listdir def classify0(inX, dataSet, labels, ...

  7. 阿里云栖大会 所有ppt

    https://github.com/Alimei/hangzhouYunQi2017ppt

  8. gdufe1538-是男人就上100层-(三维bfs)

    Problem Description: 桐老爷和UGO终于来到了名为“是男人就上一百层的塔”的下面,听说大祭司在第100层沉睡.为了题目需要,我把这个塔的层数随机打乱,层数的话大家就忘了前面的100 ...

  9. JS----文档对象模型

    DOM: document object model 文档对象模型提供了一套可以访问和修改HTML文档内容的方法 访问:获取 修改:设置 1 JS要去操作HTML元素,必须要先用JS找到他,转换为JS ...

  10. 前端三大框架之一React入门教程

    相信大家对框架这个词都很熟悉吧,我一直喜欢js原生来开发,但是目前都要求工作效率,所有使用框架或者是库会使我们开发更加方便和快速,甚至一个人干十个人的活.. 框架优点: 1.方便开发.快速写功能 2. ...