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<=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

    题解

  • 二分图最优匹配模板题
  • 用的匈牙利算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,x,y) for(register int i=x;i<=y;++i)
#define repd(i,x,y) for(register int i=x;i>=y;--i)
#define ll long long
using namespace std;
const int N=1e6+7;
template <typename T>inline void read(T &x){
char c;int sign=1;x=0;
do{c=getchar(); if(c=='-')sign=-1;}while(c<'0'||c>'9');
do{x=x*10+c-'0'; c=getchar();}while(c>='0'&&c<='9');
x*=sign;
}
struct node{
int v,nxt;
inline void init(int a,int b){v=a; nxt=b;}
}e[N];
int head[N],mark[N],vis[N],cnt=1,k,n,m,ans;
int find(int u){
for(int i=head[u];i!=-1;i=e[i].nxt)if(!vis[e[i].v]){
vis[e[i].v]=1;
if(!mark[e[i].v]||find(mark[e[i].v])){
mark[e[i].v]=u;
return 1;
}
}
return 0;
}

int main(){
while(~scanf("%d",&k)&&k){
memset(head,-1,sizeof(head));
memset(mark,0,sizeof(mark));
read(m);read(n);
rep(i,1,k){
int x,y;
read(x);read(y);
e[i].init(y,head[x]);
head[x]=i;
}
ans=0;
rep(i,1,m){
memset(vis,0,sizeof(vis));
if(find(i))ans++;
}
printf("%d\n",ans);
}
return 0;
}