HDU2063 过山车(二分图匹配+匈牙利算法)

HDU2063 过山车(二分图匹配+匈牙利算法)

Description
RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了。可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐。但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner。考虑到经费问题,boss刘决定只让找到partner的人去坐过山车,其他的人,嘿嘿,就站在下面看着吧。聪明的Acmer,你可以帮忙算算最多有多少对组合可以坐上过山车吗?
Input
输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数。0<K<=10001<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。
Output
对于每组数据,输出一个整数,表示可以坐上过山车的最多组合数。
Sample Input
6 3 3
1 1
1 2
1 3
2 1
2 3
3 1
0
Sample Output
3

题意

二分图匹配裸题

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<functional>
#include<map>
#include<unordered_map>
#define lowbit(x) ((x)&-(x));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10,NN=2e3+10,INF=0x3f3f3f3f,LEN=20;
const ll MOD=1e9+7;
const ull seed=31;
struct Edge{
	int next,to,dis;
}edge[N];
int k,m,n,num_edge;
int head[N],match[NN];
bool reserve_boy[NN];
void add_edge(int from,int to,int dis){
	edge[num_edge].next=head[from];
	edge[num_edge].to=to;
	edge[num_edge].dis=dis;
	head[from]=num_edge++;
}
bool dfs(int u){
	for(int i=head[u];i!=-1;i=edge[i].next){
		int v=edge[i].to;
		if(!reserve_boy[v]){
			reserve_boy[v]=true;
			if(!match[v]||dfs(match[v])){
				match[v]=u;
				return true;
			}
		}
	}
	return false;
}
void init(){
	num_edge=0;
	memset(head,-1,sizeof head);
	memset(match,0,sizeof match);
}
int main(){
	while(scanf("%d",&k)&&k){
		init();
		scanf("%d%d",&m,&n);
		for(int i=1;i<=k;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			add_edge(u,v,1);
		}
		int sum=0;
		for(int i=1;i<=m;i++){
			memset(reserve_boy,false,sizeof reserve_boy);
			if(dfs(i)) ++sum;
		}
		printf("%d\n",sum);
	}
}

猜你喜欢

转载自blog.csdn.net/Hc_Soap/article/details/107772306
今日推荐