标签:图论之二分匹配
6 3 3 1 1 1 2 1 3 2 1 2 3 3 1 0
3
第一道二分匹配题。。。
纯属模板。
参考:http://blog.csdn.net/wellerzhao/article/details/7756956
代码1:
#include <stdio.h> #include <string.h> #define M 555 int map[M][M]; int mx[M], my[M]; int vis[M]; int n, m; int find(int s){ int i; for(i = 1; i<= m; i ++){ if(!vis[i]&&map[s][i]){ vis[i] = 1; if(my[i] == 0||find(my[i])){ my[i] = s; mx[s] = i; return 1; } } } return 0; } void f(){ for(int i = 1; i <= n; i ++) printf("%d..%d,,%d..%d\n", i, mx[i], i, my[i]); } int main(){ int k; while(scanf("%d", &k), k){ int i; memset(map, 0, sizeof(map)); memset(mx, 0, sizeof(mx)); memset(my, 0, sizeof(my)); int a, b; scanf("%d%d", &n, &m); for(i = 0; i < k; i ++){ scanf("%d%d", &a, &b); map[a][b] = 1; } int ans = 0; for(i = 1; i<= n; i ++){ if(!mx[i]){ memset(vis, 0, sizeof(vis)); if(find(i)) ++ans; } } printf("%d\n", ans); // f(); } return 0; }
代码;
#include <stdio.h> #include <string.h> #define M 555 int map[M][M]; int mx[M], my[M]; int vis[M]; int n, m; int find(int s){ int i; for(i = 1; i<= m; i ++){ if(!vis[i]&&map[s][i]){ vis[i] = 1; if(my[i] == 0||find(my[i])){ my[i] = s; mx[s] = i; return 1; } } } return 0; } void f(){ for(int i = 1; i <= n; i ++) printf("%d..%d,,%d..%d\n", i, mx[i], i, my[i]); } int main(){ int k; while(scanf("%d", &k), k){ int i; memset(map, 0, sizeof(map)); memset(mx, 0, sizeof(mx)); memset(my, 0, sizeof(my)); int a, b; scanf("%d%d", &n, &m); for(i = 0; i < k; i ++){ scanf("%d%d", &a, &b); map[a][b] = 1; } int ans = 0; for(i = 1; i<= n; i ++){ if(!mx[i]){ memset(vis, 0, sizeof(vis)); if(find(i)) ++ans; } } printf("%d\n", ans); // f(); } return 0; }
标签:图论之二分匹配
原文地址:http://blog.csdn.net/shengweisong/article/details/40631763