Ollivanders: Makers of Fine Wands since 382 BC(HDU 1179)---二分图最大匹配数

题目链接

题目描述

In Diagon Alley ,there is only one Wand-seller,peeling gold letters over the door read Ollivanders: Makers of Fine Wands since 382 BC.A single wand lay on a faded purple cushion in the dusty window.
A tinkling bell rang somewhere in the depths of the shop as they stepped inside.It was a tiny place,empty execpt for a single spindly chair which Hagrid sat on to wait.Harry felt strangely as though he had entered a very strict library;he swallowd a log of new questions which had just occurred to him and looked instead at the thousands of narrow boxes piled neatly right up to the ceiling.For some reason,the back of his neck prickled.The very dust and silence in here seemed to tingle with some secret magic.
'Good afternoon,'said a soft voice.Harry jumped.Hagrid must have jumped,too,because there was a loud crunching noise and he got quickly off the spindly chair.
An old man was standing before them, his wide pale eyes shining like moons through the gloom of the shop.
‘Hello,’ said Harry awkwardly.
‘Ah yes,’ said the man. 'Yes,yes. I thought I’d be seeing you soon,Harry Potter.‘It wasn’t a question.You have your mother’s eyes. It seems only yesterday she was in here herself,buying her first wand. Ten and a quarter inches long, swishy, made of willow. Nice wand for charm work.’
Mor Ollivander moved closer to Harry.Harry wished he would blink.Those sivery eyes were a bit creepy.
‘Your father, on the other hand, favoured a mahogany wand.Eleven inches.Pliable.A little more power and excellent for transfiguration.Well ,I say your father favoured it - it’s really the wand that choosed the wizard, of cource.’
Yes, some wands fit some wizards ,as we all know.But what Harry doesn’t know is Ollivander have met a big trouble.That’s more than twenty years ago,When Harry’s father James Potter was still a student in Hogwarts.He went Diagon Alley to buy new books,passing by Ollivander’s shop.Ollivander was crazy for a problem:He was too busy to choose most suitable wand for every wizard.Even more,there are too many customer that day.Though Ollivader knew every wand’s favourite,he could not choose as many wizards as possible to get the wands. So James Potter,a very clever man ,gave him a magic disk with your program ,to help him sell wands as many as possible.
Please notice: one wand can only be sold to one wizard, and one wizard can only buy one wand,too.

输入格式

There are several cases. For each case, there is two integers N and M in the first line,which mean there is N wizards and M wands(0 < N <= M <= 100).
Then M lines contain the choices of each wand.The first integer in i+1th line is Ki,and after these there are Ki integers Bi,j which are the wizards who fit that wand. (0<=Ki<=N,1<=Bi,j<=N)

输出格式

Only one integer,shows how many wands Ollivander can sell.

输入样例

3 4
3 1 2 3
1 1
1 1
0

输出样例

2

分析

题目大意是给出n个巫师和m只魔杖,每个魔杖只会选择对应的巫师,且每只魔杖只选择其中一个巫师,一个巫师只选择一只魔杖,求巫师和魔杖的最大匹配数。
将魔杖作为左点集,编号为1~m;将巫师作为右点集,编号为m+1 ~m+n,套用匈牙利算法模板即可,以下分别是dfs版本和bfs版本的源代码。

源程序

DFS版本

#include <bits/stdc++.h>
#define MAXN 205
#define MAXM MAXN*MAXN
using namespace std;
struct Edge{
	int u,v,next;
	Edge(){}
	Edge(int u,int v,int next):u(u),v(v),next(next){}
}edge[MAXM];
int EdgeCount,head[MAXN];
int n,m,k,link[MAXN];
bool vis[MAXN];
void addEdge(int u,int v)
{
	edge[++EdgeCount]=Edge(u,v,head[u]);
	head[u]=EdgeCount;
}
bool dfs(int u)
{
	for(int i=head[u];i;i=edge[i].next){
		int v=edge[i].v;
		if(!vis[v]){	//不在交替路中
			vis[v]=true;
			if(link[v]==-1 || dfs(link[v])){	//未匹配点或者找到新方案
				link[v]=u;
				return true; 
			} 
		}
	}
	return false;
}
int hargarian()
{
	memset(link,-1,sizeof(link));
	int ans=0;	//记录匹配数
	for(int i=1;i<=m;i++){
		memset(vis,false,sizeof(vis));
		if(dfs(i))	//寻找匹配 
			ans++;
	} 
	return ans; 
}
int main()
{
	while(~scanf("%d%d",&n,&m)){
		memset(head,0,sizeof(head));	//初始化 
		EdgeCount=0;
		for(int u=1;u<=m;u++){
			scanf("%d",&k);
			for(int i=1;i<=k;i++){
				int tmp;
				scanf("%d",&tmp);
				addEdge(u,m+tmp);	//魔杖编号为1~m 
			}						//巫师编号为m+1~m+n
		} 
		printf("%d\n",hargarian()); //以魔杖编号为左点集,巫师编号为右点集找最大匹配 
	}
}

BFS版本

#include <bits/stdc++.h>
#define MAXN 205
#define MAXM MAXN*MAXN
using namespace std;
struct Edge{
	int u,v,next;
	Edge(){}
	Edge(int u,int v,int next):u(u),v(v),next(next){}
}edge[MAXM];
int EdgeCount,head[MAXN];
int n,m,k,pre[MAXN],vis[MAXN],link[MAXN];
queue<int> q; 
void addEdge(int u,int v)
{
	edge[++EdgeCount]=Edge(u,v,head[u]);
	head[u]=EdgeCount;
}
int hargarian()
{
	memset(pre,-1,sizeof(pre));
	memset(link,-1,sizeof(link));
	memset(vis,-1,sizeof(vis));
	int ans=0;	//记录匹配数
	for(int i=1;i<=m;i++){	
		if(link[i]==-1){	//尚未匹配
			while(!q.empty()) q.pop();
			q.push(i);
			bool flag=false;
			while(!q.empty()&&!flag){	//寻找匹配方案 
				int u=q.front();q.pop();
				for(int j=head[u];j;j=edge[j].next){
					if(flag) break;	//已找到方案
					int v=edge[j].v;
					if(vis[v]!=i){	//不在交替路中
						vis[v]=i;
						if(link[v]>=0)	//对应点已匹配
							q.push(link[v]),pre[link[v]]=u;
						else{	//找到匹配点 
							flag=true;
							int l=u,r=v;
							while(l!=-1){	//分配匹配方案 
								int tmp=link[l];
								link[l]=r;
								link[r]=l;
								l=pre[l];
								r=tmp; 
							} 
						} 
					} 
				}
			}
			if(link[i]!=-1)	//找到匹配点 
				 ans++; 
		}
	} 
	return ans;
}
int main()
{
	while(~scanf("%d%d",&n,&m)){
		memset(head,0,sizeof(head));	//初始化 
		EdgeCount=0;
		for(int u=1;u<=m;u++){
			scanf("%d",&k);
			for(int i=1;i<=k;i++){
				int tmp;
				scanf("%d",&tmp);
				addEdge(u,tmp+m);	//魔杖编号为1~m 
			}						//巫师编号为m+1~m+n 
		} 
		printf("%d\n",hargarian());	//以魔杖编号为左点集,巫师编号为右点集找最大匹配 
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值