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