Girls and Boys

点击打开链接

二分图匹配,hopcroft-karp

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int MAXN = 5010;
const int MAXM = 50010;

struct Edge{
	int to, next;
}edge[ MAXM ];

int head[ MAXN ], tot;

void init(){
	tot = 0;
	memset( head, -1, sizeof( head ) );
}

void addedge( int u, int v ){
	edge[ tot ].to = v;
	edge[ tot ].next = head[ u ];
	head[ u ] = tot++;
}

int linker[ MAXN ];
bool used[ MAXN ];
int uN;
bool dfs( int u ){
	for( int i = head[ u ]; i != -1; i = edge[ i ].next ){
		int v = edge[ i ].to;
		if( !used[ v ] ){
			used[ v ] = true;
			if( linker[ v ] == -1 || dfs( linker[ v ] ) ){
				linker[ v ] = u;
				return true;
			}
		}
	}
	return false;
}

int hungary(){
	int res = 0;
	memset( linker, -1, sizeof( linker ) );
	for( int u = 0; u < uN; ++u ){
		memset( used, false, sizeof( used ) );
		if( dfs( u ) ) 
			res++;
	}
	return res;
}

int main(){
	int n;
	while( scanf( "%d", &n ) != EOF ){
		init();
		int sum = 0;
		for( int i = 0;i < n; ++i ) {
			int temp1, temp2, temp;
			scanf( "%d: (%d)", &temp1, &temp2 );
			sum += temp2;
		//	cout << temp1 << " " << temp2 << endl;
			while( temp2-- ){
				scanf( "%d", &temp );
				addedge( temp1, temp );
			}
		}
		uN = n;
		int ans = hungary();
		cout << n - ans / 2 << endl;
	} 
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值