HDU2063+FZU2039(二分图匹配模板题)

HDU - 2063 过山车

RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了。可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐。但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner。考虑到经费问题,boss刘决定只让找到partner的人去坐过山车,其他的人,嘿嘿,就站在下面看着吧。聪明的Acmer,你可以帮忙算算最多有多少对组合可以坐上过山车吗?

Input

输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数。0<K<=1000
1<=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 <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#define ll long long
using namespace std;
const int N=510;
int head[N],cnt,match[N],vis[N],k,n,m;
struct node{ int to,next; }E[2*N]; 
void Init()
{
	cnt=0;
	for(int i=1;i<=n;i++)
		head[i]=-1;
	return ;
}
void add(int u,int v)
{
	E[cnt].to=v;
	E[cnt].next=head[u];
	head[u]=cnt++;
	return ;
}
int dfs(int x)
{
	for(int i=head[x];i!=-1;i=E[i].next)
	{
		int y=E[i].to;
		if(vis[y]) continue;
		vis[y]=1;
		if(match[y]==-1||dfs(match[y]))
		{
			match[y]=x;
			return 1;
		}	
	}
	return 0;
	
}
int hungary()
{
	int ans=0;
	for(int i=1;i<=m;i++)
		match[i]=-1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
			vis[j]=0;
		if(dfs(i)) ans++;
	}
	return ans;
}
int main(void)
{
	while(scanf("%d",&k)&&k) 
	{
		scanf("%d%d",&n,&m);
		Init();
		while(k--)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v); 
		}
		printf("%d\n",hungary());		
	}
	return 0;	
} 

FZU 2039  Pets

Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There are totally m pets in the shop, numbered from 1 to m. One day, there are n customers in the shop, which are numbered from 1 to n. In order to sell pets to as more customers as possible, each customer is just allowed to buy at most one pet. Now, your task is to help the manager to sell as more pets as possible. Every customer would not buy the pets he/she is not interested in it, and every customer would like to buy one pet that he/she is interested in if possible.

 

Input

There is a single integer T in the first line of the test data indicating that there are T(T≤100) test cases. In the first line of each test case, there are three numbers n, m(0≤n,m≤100) and e(0≤e≤n*m). Here, n and m represent the number of customers and the number of pets respectively.

In the following e lines of each test case, there are two integers x(1≤x≤n), y(1≤y≤m) indicating that customer x is not interested in pet y, such that x would not buy y.

Output

For each test case, print a line containing the test case number (beginning with 1) and the maximum number of pets that can be sold out.

Sample Input

1
2 2 2
1 2
2 1

Sample Output

Case 1: 2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#define ll long long
using namespace std;
const int N=110;
int vis[N],match[N];
bool book[N][N];
int n,m;
void Init()
{
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			book[i][j]=1;
	for(int i=1;i<=m;i++)
		match[i]=-1;
	return ;
}
int dfs(int x)
{
	for(int i=1;i<=m;i++)
	{
		if(!book[x][i]||vis[i])
			continue;
		vis[i]=1;
		if(match[i]==-1||dfs(match[i]))
		{
			match[i]=x;
			return 1;
		}
	}
	return 0;
}
int hungary()
{
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
			vis[j]=0;
		if(dfs(i)) ans++; 
	}
	return ans;
}
int main(void)
{
	int t,tt=0;
	scanf("%d",&t);
	while(t--)
	{
		int e;
		scanf("%d%d%d",&n,&m,&e);
		Init();		
		while(e--)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			book[u][v]=0;
		}
		printf("Case %d: %d\n",++tt,hungary());	
	}
	
	
	return 0;	
} 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值