描述

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. 机器学习进阶-图像特征harris-角点检测 1.cv2.cornerHarris(进行角点检测)

    1.cv2.cornerHarris(gray, 2, 3, 0.04)  # 找出图像中的角点 参数说明:gray表示输入的灰度图,2表示进行角点移动的卷积框,3表示后续进行梯度计算的sobel算子 ...

  2. Spring boot 自定义starter

    以下配置来自尚硅谷.. 常用如何配置 @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigur ...

  3. idea debug只断点当前线程,不阻塞其他线程

    公司前后端分离,后端人员无需编写前端js ,后端开发调试某个数据的时候,前端总是嫌弃后端断点,影响到他开发.....idea早已提供这个功能,做下记录 选中你需要调试的控制器,其他控制器不会受到影响, ...

  4. ubuntu 安装 oracle sql developer

    安装java-jdk 这一部分教程很多,可按照网上教程进行安装 安装sqldeveloper 下载 sqldeveloper Linux RPM http://www.oracle.com/techn ...

  5. JSP基本_JSTL

    自定义标签是,用户定义自己的处理的tag的机制. JSTL是,JSP用标准自定义标签.从JSTL Ver.1.2开始成为JavaEE5的子集.比较有名的是Glassfish.Tomcat上开发的话,需 ...

  6. 混合app开发,h5页面调用ios原生APP的接口

    混合APP开发中,前端开发H5页面,不免会把兼容性拉进来,在做页面的兼容性同事,会与原生app产生一些数据交互: 混合APP开发,安卓的兼容性倒是好说,安卓使用是chrome浏览器核心,已经很好兼容H ...

  7. JAVAWEB 一一 Spirng(框架,IOC控制反转,DI依赖注入)

    jar包 applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <bea ...

  8. 使用__future__实现从python2.7到python3.x的过渡

    参考链接:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820023 ...

  9. SQL--结构化的查询语言

    SQL--结构化的查询语言T-SQL:Transact-SQL (SQL的增强版) 逻辑运算符 and && or || not ! 关系运算符 等于 = 不等于<>或!= ...

  10. swift视图的添加及层次变动和基本动画

    // 一般的我们添加一个视图到父视图都是通过 /* let v1 = UIView(frame:CGRectMake(100,200,30,50)) self.view.addSubview(v1) ...