题目链接:https://vjudge.net/problem/HDU-1083

Courses

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8869    Accepted Submission(s): 4319

Problem Description
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:

 
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
 
Source

题解:

有n个学生,m门选修课。给出选课情况,问能否为每门科目安排一位课代表?

求出最大匹配数,即一门选修课与一个学生匹配。如果最大匹配数等于选修课的数目,则可以为每门科目安排一位课代表;否则不能。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = +; int uN, vN;
char a[MAXN][MAXN];
int M[MAXN][MAXN], link[MAXN];
bool vis[MAXN]; bool dfs(int u)
{
for(int i = ; i<=vN; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=uN; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &uN, &vN);
memset(M, false, sizeof(M));
for(int i = ; i<=uN; i++)
{
int m, v;
scanf("%d", &m);
while(m--)
{
scanf("%d", &v);
M[i][v] = true;
}
} int cnt = hungary();
if(cnt==uN) puts("YES");
else puts("NO");
}
}

最新文章

  1. 第一篇博客:Hello World
  2. 功能齐全的图表库 ACharts
  3. 腾讯云CentOS 安装MediaWiki
  4. python 输入和输出
  5. 异常:Struts:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find BasicDataSource
  6. Math.Round函數
  7. 【收藏】Linux添加/删除用户和用户组
  8. ubuntu java jdk安装及环境变量设置
  9. CSS知识总结之浏览器(持续更新)
  10. 转载--使用image-set来优化在retian屏幕下的背景图片
  11. contenteditable实现可编辑的HTML标签
  12. python学习:Dmidecode系统信息(一)
  13. SystemVerilog语言简介(二)
  14. Docker安装步骤
  15. shiro源码篇 - shiro的session的查询、刷新、过期与删除,你值得拥有
  16. 静态库lib和动态库dll相关总结
  17. Oracle12C版本安装步骤
  18. 游戏1:HTML5制作网页游戏围住神经猫--createjs
  19. LeetCode Best Time to Buy and Sell Stock II (简单题)
  20. Java基础--面向对象以及相关知识

热门文章

  1. 让你系统的了解shell
  2. 【06】Firebug记录Javascript日志
  3. 大数据学习——hdfs集群启动
  4. HDU 1102 Kruscal算法
  5. react.js 父子组件数据绑定实时通讯
  6. 《计算机网络课程设计》基本操作(基于Cisco Packet Tracer)
  7. 使现有的VSCode成为便携版(绿色版)
  8. msp430入门编程24
  9. 魔咒词典--hdu1880(字符串 暴力)
  10. Java中设置Session过期时间(Spring Boot)