HDU-----(1083)Courses(最大匹配)
Courses
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3767 Accepted Submission(s): 1800
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
给定一个课程数目和每一个门课程听课的人的编号,问能否每一门课都有人听课...!
代码:
1 #include<cstdio>
2 #include<cstring>
3 #include<vector>
4 using namespace std;
5 const int maxn=301;
6 vector<int>mat[maxn];
7 int judge[maxn];
8 bool vis[maxn];
9 bool skm(int x){
10 vector<int>::iterator it;
11 for(it=mat[x].begin();it<mat[x].end();it++)
12 {
13 if(!vis[*it])
14 {
15 vis[*it]=1;
16 if(judge[*it]==0||skm(judge[*it]))
17 {
18 judge[*it]=x;
19 return 1;
20 }
21 }
22 }
23 return 0;
24 }
25
26 int main()
27 {
28 int cas,n,m,t,a,i;
29 scanf("%d",&cas);
30 while(cas--)
31 {
32 scanf("%d%d",&n,&m);
33 memset(judge,0,sizeof(int)*(m+2));
34 for( i=1;i<=n;i++)
35 {
36 mat[i].clear();
37 scanf("%d",&t);
38 while(t--){
39 scanf("%d",&a);
40 mat[i].push_back(a);
41 }
42 }
43 for(i=1;i<=n;i++){
44 memset(vis,0,sizeof(bool)*(m+2));
45 if(!skm(i))break;
46 }
47 if(i>n) printf("YESn");
48 else printf("NOn");
49 }
50 return 0;
51 }
采用邻接矩阵做:
1 #include<cstdio>
2 #include<cstring>
3 #include<vector>
4 #include<cstdlib>
5 #include<iostream>
6 using namespace std;
7 const int maxn=505;
8 bool mat[maxn][maxn];
9 int judge[maxn];
10 bool vis[maxn];
11 int n,m;
12 bool skm(int x){
13 for(int i=1;i<=m;i++)
14 {
15 if(!vis[i]&&mat[x][i])
16 {
17 vis[i]=1;
18 if(judge[i]==0||skm(judge[i]))
19 {
20 judge[i]=x;
21 return 1;
22 }
23 }
24 }
25 return 0;
26 }
27 int main()
28 {
29 int cas,t,a,i;
30 //freopen("test.in","r",stdin);
31 scanf("%d",&cas);
32 while(cas--)
33 {
34 scanf("%d%d",&n,&m);
35 memset(judge,0,sizeof(judge));
36 memset(mat,0,sizeof(mat));
37 for( i=1;i<=n;i++)
38 {
39 scanf("%d",&t);
40 while(t--)
41 {
42 scanf("%d",&a);
43 mat[i][a]=1;
44 }
45 }
46 for(i=1;i<=n;i++){
47 memset(vis,0,sizeof(vis));
48 if(!skm(i))break;
49 }
50 if(i>n)
51 printf("YESn");
52 else
53 printf("NOn");
54 }
55 return 0;
56 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android 线程之自定义带消息循环Looper的实例
- 详解Androidstudio3.0 关于Gradle报错的问题(小结)
- Android开发实现文件关联方法介绍
- Android开发获取重力加速度和磁场强度的方法
- Android自定义View圆形和拖动圆跟随手指拖动
- 简单好用的Adapter—ArrayAdapter详解
- Android开发之图片切割工具类定义与用法示例
- Android开发之超强图片工具类BitmapUtil完整实例
- Android使用URL读取网络资源的方法
- Android控件RecyclerView实现混排效果仿网易云音乐
- MUI整合上拉下拉的写法
- 详解OkSocket与Android的简单使用
- 浅谈Android Studio 3.0 的一些小变化
- 详解如何使用Android Studio开发Gradle插件
- Android ItemDecoration 实现分组索引列表的示例代码