【HDU】2458 Kindergarten 二分匹配

在幼儿园中,孩子们分为女孩和男孩,女孩之间、男孩之间以及部分女孩和男孩之间相互认识。教师需要从这些孩子中选出尽可能多的人进行游戏,但选出的人必须相互认识。本文详细介绍了如何通过构建补图并求最大匹配来解决这一问题。

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

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 3 1 1 1 2 2 3 2 3 5 1 1 1 2 2 1 2 2 2 3 0 0 0
 

Sample Output
  
Case 1: 3 Case 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 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值