描述

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. JAVA8-待续

    1. 函数式编程,因为在并发和时间驱动编程中的优势,函数式编程又逐渐流行起来 以前是实现一个比较器需要实现Comparator接口,并重写compare方法,以下为两种实现方法(类似还有线程,事件等) ...

  2. 【389】Implement N-grams using NLTK

    Ref: Natural Language Toolkit Ref: n-grams in python, four, five, six grams? Ref: "Elegant n-gr ...

  3. 遍历DOM树,获取父节点

    通过获取父节点,还可以获取父节点的父节点. 有3个常用方法: 方法  说明  parent()  选取父节点  parents()  选取所有父节点  parentsUntil("div&q ...

  4. week05 05restful api

    和第一个项目一样 然后去App.js注册一下 但是呢 新闻是写死在 现在主要输调通前端client和后端server 持续获取新闻 至于真假先不考虑 下面我们回到前端NewsPanel 这个reque ...

  5. ORACLE数据库 常用命令和Sql常用语句

    ORACLE 账号相关 如何获取表及权限 1.COPY表空间backup scottexp登录管理员账号system2.创建用户 create user han identified(认证) by m ...

  6. Hibernate 再接触 基础配置 搭建Log4j环境 Junit日志环境等

    <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.aut ...

  7. Html - Table 表头固定和 tbody 设置 height 在IE不起作用的解决

    原文地址,转载请注明出处:http://www.cnblogs.com/jying/p/6294063.html 做项目的时候发现给 tbody设置 height 和 overflow-y 在IE下不 ...

  8. C#中的 new Random()

    在C#中,产生随机数常用大方法是 new Random().Next(1,10)等方法. 但是仔细发现会有个问题: 看代码: ; i < ;i++ ) { Console.WriteLine(, ...

  9. Python unindent dese not match any out indentation level 问题

    今天写个小程序出现 “unindent dese not  match any out indentation level”. 一直没找到原因,经过仔细对比发现实际上是缩进的问题. 上下两行的缩进用的 ...

  10. idea git 发起一个pull request 请求