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");
}
}
HDU1083 Courses —— 二分图最大匹配的更多相关文章
- HDU-1083 Courses 二分图 最大匹配
题目链接:https://cn.vjudge.net/problem/HDU-1083 题意 有一些学生,有一些课程 给出哪些学生可以学哪些课程,每个学生可以选多课,但只能做一个课程的代表 问所有课能 ...
- HDU1083(KB10-C 二分图最大匹配)
Courses Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- POJ 1469 COURSES 二分图最大匹配 二分图
http://poj.org/problem?id=1469 这道题我绝壁写过但是以前没有mark过二分图最大匹配的代码mark一下. 匈牙利 O(mn) #include<cstdio> ...
- [POJ] 2239 Selecting Courses(二分图最大匹配)
题目地址:http://poj.org/problem?id=2239 Li Ming大学选课,每天12节课,每周7天,每种同样的课可能有多节分布在不同天的不同节.问Li Ming最多可以选多少节课. ...
- POJ2239_Selecting Courses(二分图最大匹配)
解题报告 http://blog.csdn.net/juncoder/article/details/38154699 题目传送门 题意: 每天有12节课.一周上7天,一门课在一周有多天上课. 求一周 ...
- poj2239 Selecting Courses --- 二分图最大匹配
匈牙利算法模板题 有n门课程,每门课程可能有不同一时候间,不同一时候间的课程等价. 问不冲突的情况下最多能选多少门课. 建立二分图,一边顶点表示不同课程,还有一边表示课程的时间(hash一下). #i ...
- HDU1083(二分图最大匹配vector实现)
Courses Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- poj 2239 二分图最大匹配,基础题
1.poj 2239 Selecting Courses 二分图最大匹配问题 2.总结:看到一个题解,直接用三维数组做的,很巧妙,很暴力.. 题意:N种课,给出时间,每种课在星期几的第几节课上 ...
- POJ2239 Selecting Courses(二分图最大匹配)
题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 #include <iostream ...
随机推荐
- HDU-1232/NYOJ-608畅通工程,并查集模板题,,水过~~~
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) http://acm. ...
- Problem 2121 神庙逃亡(FZU)
Problem 2121 神庙逃亡 Accept: 700 Submit: 1788 Time Limit: 1000 mSec Memory Limit : 32768 KB Prob ...
- HDU1711 最基础的kmp算法
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- hdu 1421经典dp
#include<stdio.h> #include<stdlib.h> #define N 2001 #define inf 0x3fffffff int a[N],dp[N ...
- hdu 1564水题Play a game
#include<stdio.h> int main() { int n; while(scanf("%d",&n),n) { n=n*n-1; i ...
- hdu 2686最小费用最大流问题
#include<stdio.h> #include<queue> #include<string.h> using namespace std; #define ...
- ascii 和 byte以及UTF-8的转码规则
多年来闲麻烦,只记录笔记,不曾编写BLOG,本文为原创,如需转载请标明出处 废话不说,直奔主题 ascii 计算机只接受 “高”.“低”电压,所以使用二进制 1 和 0 分别代表高低电压 ...
- Android操作系统架构
Android操作系统架构 Android操作系统整体应用架构 Android系统架构和一些普遍的操作系统差不多,都是采用了分层的架构,从他们之间的架构图看,Android系统架构分为四个层,从高 ...
- 安装redis和phpredis模块
安装redis shell> wget http://redis.googlecode.com/files/redis-2.0.4.tar.gz shell> tar zxvf redis ...
- HDU 1074 Doing Homework【状态压缩DP】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...