博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hdu 1803 Courses 二分图匹配模板题
阅读量:3904 次
发布时间:2019-05-23

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

题目:

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:

Input

23 33 1 2 32 1 21 13 32 1 32 1 31 1

Output

YESNO

Sample Input

23 33 1 2 32 1 21 13 32 1 32 1 31 1

Sample Output

YESNO

代码如下:

#include 
#include
#include
#include
using namespace std;const int maxn=105;int t;int a[maxn][3*maxn];int match[3*maxn];int vis[3*maxn];int n,m;bool Find(int x){ for (int i=1;i<=m;i++) { if(a[x][i]&&!vis[i]) { vis[i]=1; if(match[i]==-1||Find(match[i])) { match[i]=x; return true; } } } return false;}int arrage(){ int sum=0; for (int i=1;i<=n;i++) { memset (vis,0,sizeof(vis)); if(Find(i)) sum++; } return sum;}int main(){ scanf("%d",&t); while(t--) { memset (match,-1,sizeof(match)); memset (a,0,sizeof(a)); scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { int w; scanf("%d",&w); while(w--) { int x; scanf("%d",&x); a[i][x]=1; } } if(arrage()==n) printf("YES\n"); else printf("NO\n"); } return 0;}

 

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

你可能感兴趣的文章
程序员应该投资的10件事
查看>>
多媒体
查看>>
沟通技巧
查看>>
专业camera/isp术语中英文对照
查看>>
摄像头
查看>>
我的理想,我的奋斗目标
查看>>
Nginx基于多域名、多端口、多IP配置虚拟主机
查看>>
一次Linux 系统受攻击的解决过程
查看>>
最新最全Apache源码编译安装
查看>>
最新mysql数据库源码编译安装。
查看>>
第一章 vue入门
查看>>
Linux文件引用计数的逻辑
查看>>
linux PCIe hotplug arch analysis
查看>>
LDD3 study note 0
查看>>
cpio compress and extract
查看>>
PCI SMMU parse in ACPI
查看>>
const使用实例
查看>>
面向对象设计案例——点和圆关系
查看>>
深拷贝与浅拷贝
查看>>
WinForm 打开txt文件
查看>>