POJ-1469 COURSES ( 匈牙利算法 dfs + bfs )

题目链接: http://poj.org/problem?id=1469

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 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抣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

模板题,使用匈利亚算法找最大匹配,匈利亚算法有dfs和bfs两种实现,dfs代码短,容易理解,bfs有点复杂,据说速度要快一点,我两种都写了一下。


 1 //dfs
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6 
 7 int p, n;
 8 bool visit[105][305];
 9 int stop[305];
10 bool used[305];
11 
12 bool search( int x ){
13     for( int i = 1; i <= n; i++ )
14         if( visit[x][i] && !used[i] ){
15             used[i] = true;
16             if( stop[i] == 0 || search( stop[i] ) ){
17                 stop[i] = x;
18                 return true;
19             }
20         }
21 
22     return false;
23 }
24 
25 int main(){
26     int T;
27     scanf( "%d", &T );
28     while( T-- ){
29         memset( visit, false, sizeof( visit ) );
30         memset( stop, 0, sizeof( stop ) ); 
31         scanf( "%d%d", &p, &n );
32         int flag = true;
33 
34         for( int i = 1; i <= p; i++ ){
35             int temp, a;
36             scanf( "%d", &temp );
37             while( temp-- ){
38                 scanf( "%d", &a );
39                 visit[i][a] = true;
40             }
41         }
42 
43         for( int i = 1; i <= p; i++ ){
44             if( !flag ) continue;
45             else{
46                 memset( used, false, sizeof( used ) );
47                 if( !search( i ) ){
48                     flag = false; 
49                 }
50             }
51         }
52 
53         if( flag ) printf( "YES
" );
54         else printf( "NO
" );
55     }
56 
57     return 0;
58 }

 1 //bfs
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<queue>
 6 using namespace std;
 7 
 8 int n, p;
 9 bool visit[105][305];
10 int ptos[105], stop[305], pre[105];//ptos,stop记录匹配,pre记录课程匹配时的前驱点
11 int flag[305];//寻找增广路时判断是否已经添加过
12 
13 int maxmatch(){
14     int ans = 0;
15     memset( ptos, 0, sizeof( ptos ) );
16     memset( stop, 0, sizeof( stop ) );
17     memset( flag, false, sizeof( flag ) );
18 
19     for( int i = 1; i <= p; i++ ){
20 
21         if( ptos[i] == 0 ){//没有匹配
22             queue<int> Q;
23             Q.push( i );
24             pre[i] = 0;
25             bool ok = false;//判断是否找到增广路
26 
27             while( !Q.empty() && !ok ){//没有找到增广路
28                 int u = Q.front();
29                 Q.pop();
30 
31                 for( int k = 1; k <= n && !ok; k++ ){//开始寻找增广路
32                     if( visit[u][k] && flag[k] != i ){
33                         flag[k] = i;
34                         Q.push( stop[k] );
35                         if( stop[k] == 0 ){
36                             ok = true;
37                             int tempp = u,temps = k;
38                             while( tempp != 0 ){//改变匹配
39                                 int x = ptos[tempp];
40                                 ptos[tempp] = temps;
41                                 stop[temps] = tempp;
42                                 tempp = pre[tempp];
43                                 temps = x;
44                             } 
45                         }
46                         else pre[stop[k]] = u;
47                     }
48                 }
49             }
50             if( ptos[i] != 0 ) ans++;
51         }
52     }
53 
54     return ans;
55 }
56 
57 int main(){
58     int T;
59     scanf( "%d", &T );
60     while( T-- ){
61         memset( visit, false, sizeof( visit ) );
62         scanf( "%d%d", &p, &n );
63 
64         int temp, a;
65         for( int i = 1; i <= p; i++ ){
66             scanf( "%d", &temp );
67             while( temp-- ){
68                 scanf( "%d", &a );
69                 visit[i][a] = true;
70             }
71         }
72 
73         if( p == maxmatch() ) printf( "YES
" );
74         else printf( "NO
" );
75     }
76     
77     return 0;
78 }



原文地址:https://www.cnblogs.com/hollowstory/p/5348154.html