博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1083——Courses(匈牙利算法)
阅读量:2344 次
发布时间:2019-05-10

本文共 2683 字,大约阅读时间需要 8 分钟。

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

求课程与人数能不能组成一一对应的关系,其实就是求二分图的最大匹配是否等于课程数

#include 
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#define INF 0x3f3f3f3f#define MAXN 305#define Mod 10001using namespace std;bool vis[MAXN];int n,p;int mp[MAXN][MAXN],pre[MAXN]; //匹配路径;int find(int cur){ for(int i=1; i<=n; ++i) { if(!vis[i]&&mp[cur][i]) { vis[i] = true; if(pre[i] == 0 || find(pre[i])) { pre[i]=cur; return 1; } } } return 0;}int main(){ int t; scanf("%d",&t); while(t--) { memset(mp,0,sizeof(mp)); memset(pre,0,sizeof(pre)); scanf("%d%d",&p,&n); for(int i=1;i<=p;++i) { int cnt,x; scanf("%d",&cnt); while(cnt--) { scanf("%d",&x); mp[i][x]=1; } } int ans=0; for(int i=1;i<=p;++i) { memset(vis,0,sizeof(vis)); if(find(i)) ans++; } if(ans==p) printf("YES\n"); else printf("NO\n"); } return 0;}

转载地址:http://pvcvb.baihongyu.com/

你可能感兴趣的文章
Copy_from&to_user详解
查看>>
【C++】六、继承与多态
查看>>
特征向量的欧式距离与余弦距离——推荐算法
查看>>
jQuery提示和技巧
查看>>
是否可以在Python中将长行分成多行[重复]
查看>>
你什么时候使用Builder模式? [关闭]
查看>>
在jQuery中每5秒调用一次函数的最简单方法是什么? [重复]
查看>>
Angular 2+中的ngShow和ngHide等效于什么?
查看>>
如何将Java String转换为byte []?
查看>>
@Transactional注释在哪里?
查看>>
找不到Gradle DSL方法:'runProguard'
查看>>
AngularJS ngClass条件
查看>>
为什么需要在脚本文件的开头加上#!/ bin / bash?
查看>>
ReactJS-每次调用“ setState”时都会调用渲染吗?
查看>>
ng-if和ng-show / ng-hide有什么区别
查看>>
用Java复制文件的标准简洁方法?
查看>>
管理webpack中的jQuery插件依赖项
查看>>
删除可能不存在的文件的大多数pythonic方式
查看>>
如何在Eclipse中为Java文本编辑器更改字体大小?
查看>>
我们应该@Override接口的方法实现吗?
查看>>