hdu2063 二分图的匈牙利匹配

过山车

                                                            Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

                                                                                Total Submission(s): 4769    Accepted Submission(s): 2064

Problem 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
 
Author
PrincessSnow
 
Source
第一个二分图,套的模版……come on……
View Code
 1 //适用于边数比较多的图
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <iostream>
 5 using namespace std;
 6 const int MAXN=500;
 7 bool bmap[MAXN][MAXN];
 8 bool bmask[MAXN];
 9 int nx,ny;
10 int cx[MAXN];
11 int cy[MAXN];
12 int findpath(int u)
13 {
14     int i,j;
15     for(i=0;i<ny;i++)
16     {
17         if(bmap[u][i]&&!bmask[i])
18         {
19             bmask[i]=1;
20             if(cy[i]==-1||findpath(cy[i]))
21             {
22                 cy[i]=u;
23                 return 1;
24             }
25         }
26     }
27     return 0;
28 }
29 int MaxMatch()
30 {
31     int res=0;
32     int i,j;
33     for(j=0;j<ny;j++)
34        cy[j]=-1;
35     for(i=0;i<nx;i++)
36     {
37         memset(bmask,0,sizeof(bmask));
38         res+=findpath(i);
39     }
40     return res;
41 }
42 int main()
43 {
44     int t,a,b;
45     while(scanf("%d",&t),t)
46     {
47         scanf("%d %d",&nx,&ny);
48         memset(bmap,0,sizeof(bmap));
49         for(int i=0;i<t;i++)
50         {
51             scanf("%d%d",&a,&b);
52             bmap[a-1][b-1]=1;
53         }
54         printf("%d\n",MaxMatch());
55     }
56 }

 

posted @ 2012-07-25 19:53  _sunshine  阅读(273)  评论(0编辑  收藏  举报