码迷,mamicode.com
首页 > 编程语言 > 详细

hihocoder 1122最大二分匹配匈牙利算法

时间:2015-07-31 23:35:01      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:匈牙利算法

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
struct node{
    int from,to,next;
};
node es[10005];
int head[1005];
int cnt;
int vis[1005];
int c[1005];
int n,m;
int add(int from,int to){
    es[cnt].from = from ;
    es[cnt].to = to;
    es[cnt].next = head[from];
    head[from] = cnt++;
}
int path(int x){
    for(int t = head[x];t != 0;t = es[t].next){
        if(!vis[es[t].to]){
            vis[es[t].to] = 1;
            if(!c[es[t].to] || path(c[es[t].to])){
                c[es[t].to] = x;
                c[x] = es[t].to;
                return 1;
            }
        }
    }
    return 0;
}
int max_flow(){
    memset(c,0,sizeof(c));
    int ans = 0;
    for(int i = 1;i <= n;i++){
        if(!c[i]){
            memset(vis,0,sizeof(vis));
            ans += path(i);
        }
    }
    return ans;
}
int main(){
    while(cin >> n >> m){
        memset(head,0,sizeof(head));
        cnt = 1;
        for(int i = 0;i < m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        cout << max_flow() << endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hihocoder 1122最大二分匹配匈牙利算法

标签:匈牙利算法

原文地址:http://blog.csdn.net/qq_24667639/article/details/47176819

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!