http://acm.hdu.edu.cn/showproblem.php?pid=2063
二分图最大匹配,匈牙利算法(dfs+邻接矩阵)
月老要是用这算法。。。“一入此门深似海,从此节操是路人”,其ACM之谓也?
1 #include <stdio.h> 2 #include <string.h> 3 #define N 521 4 5 int n, m, k; 6 int map[N][N], girl[N], flag[N]; 7 8 int find(int x) 9 { 10 int i; 11 for(i=1; i<=m; i++) 12 { 13 if(!flag[i] && map[x][i]) 14 { 15 flag[i] = 1; 16 if(girl[i]==-1 || find(girl[i])) 17 { 18 girl[i] = x; 19 return 1; 20 } 21 } 22 } 23 return 0; 24 } 25 26 int main() 27 { 28 int i, a, b, sum; 29 while(scanf("%d", &k), k) 30 { 31 memset(map, 0, sizeof(map)); 32 memset(girl, -1, sizeof(girl)); 33 scanf("%d%d", &n, &m); 34 for(i=0; i<k; i++) 35 { 36 scanf("%d%d", &a, &b); 37 map[a][b] = 1; 38 } 39 sum = 0; 40 for(i=1; i<=n; i++) 41 { 42 memset(flag, 0, sizeof(flag)); 43 sum += find(i); 44 } 45 printf("%d\n", sum); 46 } 47 return 0; 48 }