POJ 1469 COURSES 解题报告

本文针对POJ1469题目,通过构建二分图进行最大匹配算法解决课程与学生间的匹配问题。文章详细介绍了使用临界表实现匈牙利算法的过程,并附上了参考代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分类:图论,最大匹配,二分图
作者: ACShiryu
时间:2011-8-1
COURSES
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 10733Accepted: 4204

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 

Input

Your program should read sets of data from the std input. The first line of the input contains the number of the data sets. Each data set is presented in the following format: 

P N 
Count1 Student 1 1 Student 1 2 ... Student 1 Count1 
Count2 Student 2 1 Student 2 2 ... Student 2 Count2 
... 
CountP Student P 1 Student P 2 ... Student P 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抣l 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. 

Output

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.

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
题目大意就是n个学生去p个课堂,每一个学生都有自己的课堂,并且每个学生只能去一个课堂,题目要求能够安排每一个课堂都有人吗?
输入数据的第一行是测试数据的个数,每组测试数据的开始分别是p和n,接着p行,每行的开始是这个课堂的学生人数m,接着m个数代表该课堂的学生编号,对于输出,如果该组数据能够这样安排就输出YES,否则输出NO。
例如,对于第一组数据明显可以这样匹配,3-3,2-2,1-1,而对于第二组数据则无法找到匹配方案,

这题明显的求二分图的最大匹配,关于该算法详见POJ 1274 The Perfect Stall 解题报告

但做这题的时候,用临界矩阵做刚开始时数组开小了,RE了一次,第二次TLE,后改为临界表,依旧TLE,最后,无奈,把cin全换成scanf时过了,在此要感谢laputa大神的提醒,Orz!

果然就过了!

2011080200361798.png

参考代码:

 1 #include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 const int maxn = 301*101;
9 int first[maxn],next[maxn],v[maxn];//头结点,下一点,和边的终点(即学生编号)
10 bool vis[maxn]; //记录是否访问过
11 int link [maxn];//记录与之匹配的点
12 int p , n ;
13 bool find ( int k )
14 {
15 int i ;
16 for ( i = first[k] ; i != -1 ; i =next [i] )
17 {//寻找与k连接的点
18 if ( !vis [v[i]] )
19 {//如果没访问过,则访问并标记
20 vis [ v[i]] =true ;
21 if ( link [v[i]] == 0 || find ( link [v[i]] ) )
22 {//如果该学生还未匹配课堂或存在增广路
23 link [v[i]] = k ;//与之匹配
24 return true ;
25 }
26 }
27 }
28 return false;
29 }
30 int main()
31 {
32 int t ;
33 scanf("%d",&t) ;
34 while ( t -- )
35 {
36 scanf("%d%d",&p,&n) ;
37 if ( p > n ) //如果学生人数都少于课堂数,明显不可能达到匹配
38 cout <<"NO"<<endl;
39 else
40 {
41 int i , j ;
42 memset ( first , -1 , sizeof ( first ) ); //初始化表头
43 memset ( link , 0 , sizeof ( link ) ) ; //初始化
44 int e = 0 ;
45
46 for ( i = 0 ; i < p ; i ++ )
47 {
48 int a ;
49 scanf("%d",&a);
50 for ( j = 0 ; j < a ; j ++ , e ++ )
51 {
52 scanf("%d",&v[e]);
53 next [e] = first [i+1]; //插入链表,从头端插入
54 first [i+1] = e ; //记录链表头结点
55 }
56 }
57 bool ans = 0 ;
58 for ( i = 0 ; i < p ; i ++ )
59 {
60 memset ( vis , 0 , sizeof ( vis ) ) ;
61 if ( !find ( i + 1 ) )
62 {//如果找不到与改点匹配的点,则答案就为NO
63 ans =true;
64 break;
65 }
66 }
67 if ( ans )
68 cout<<"NO"<<endl;
69 else
70 cout<<"YES"<<endl;
71 }
72 }
73 return 0;
74 }

 

转载于:https://www.cnblogs.com/ACShiryu/archive/2011/08/02/2124431.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值