【HDU】2458 Kindergarten 二分匹配

来源:互联网 发布:新手开淘宝 编辑:程序博客网 时间:2024/04/29 20:08

Kindergarten

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 652    Accepted Submission(s): 369


Problem Description
In a kindergarten, there are a lot of kids. All girls of the kids know each other and all boys also know each other. In addition to that, some girls and boys know each other. Now the teachers want to pick some kids to play a game, which need that all players know each other. You are to help to find maximum number of kids the teacher can pick.
 

Input
The input consists of multiple test cases. Each test case starts with a line containing three integers
G, B (1 ≤ G, B ≤ 200) and M (0 ≤ M ≤ G × B), which is the number of girls, the number of boys and
the number of pairs of girl and boy who know each other, respectively.
Each of the following M lines contains two integers X and Y (1 ≤ X≤ G,1 ≤ Y ≤ B), which indicates that girl X and boy Y know each other.
The girls are numbered from 1 to G and the boys are numbered from 1 to B.

The last test case is followed by a line containing three zeros.
 

Output
For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the maximum number of kids the teacher can pick.
 

Sample Input
2 3 31 11 22 32 3 51 11 22 12 22 30 0 0
 

Sample Output
Case 1: 3Case 2: 4
 

Source
2008 Asia Hefei Regional Contest Online by USTC

传送门:【HDU】2458 Kindergarten

题目大意:一个花园中有G个女孩,B个男孩,其中女孩之间相互认识,男孩之间也相互认识,现在给你M对关系(x,y),表示女孩x和男孩y认识,现在要从这些孩子中挑选出尽量多的孩子做游戏,但是必须保证这些孩子相互认识。

题目分析:我们假设如果存在关系(x,y)则存在边(x,y),通过这个我们得到原图,然后对原图构造补图,这样,如果女孩x'和男孩y'不认识,我们建边(x',y')。然后对补图求最大匹配,得到的是补图的最小点覆盖数ANS,也就是原图的最大独立集数ANS,那么结果就是G+B-ANS。为什么这样可以?因为把最大独立集从原图中删去,剩下的必定是两两相互认识的。

代码如下:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REPV( i , a , b ) for ( int i = a ; i >= b ; -- i )#define clear( a , x ) memset ( a , x , sizeof a )const int MAXN = 205 ;const int MAXE = 40000 ;struct Edge {int v , n ;} ;Edge edge[MAXE] ;int adj[MAXN] , cntE ;int G[MAXN][MAXN] ;bool vis[MAXN] ;int link[MAXN] ;int g , b , m ;void addedge ( int u , int v ) {edge[cntE].v = v ; edge[cntE].n = adj[u] ; adj[u] = cntE ++ ;}int find ( int u ) {for ( int i = adj[u] ; ~i ; i = edge[i].n )if ( !vis[edge[i].v] ) {int v = edge[i].v ;vis[v] = 1 ;if ( !~link[v] || find ( link[v] ) ) {link[v] = u ;return 1 ;}}return 0 ;}int match () {int ans = 0 ;clear ( link , -1 ) ;REPF ( i , 1 , g ) {clear ( vis , 0 ) ;ans += find ( i ) ;}return ans ;}void work () {int cas = 0 ;int u , v ;while ( ~scanf ( "%d%d%d" , &g , &b , &m ) && ( g || b || m ) ) {printf ( "Case %d: " , ++ cas ) ;cntE = 0 ;clear ( G , 0 ) ;clear ( adj , -1 ) ;REP ( i , m ) {scanf ( "%d%d" , &u , &v ) ;G[u][v] = 1 ;}REPF ( i , 1 , g )REPF ( j , 1 , b )if ( !G[i][j] )addedge ( i , j ) ;printf ( "%d\n" , g + b - match () ) ;}}int main () {work () ;return 0 ;}


0 0
原创粉丝点击