匈牙利算法模板

二分图的最大匹配问题n

二分图最小定点覆盖n hdu1150

DAG最小路径覆盖 m(节点数)-n

二分图最大独立子集 m-n

//匈牙利算法模板
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>

const int inf = (1<<31)-1;
const int MAXN = 1e3;
using namespace std;

int g[MAXN][MAXN];
int pre[MAXN]; //B的A节点
int vist[MAXN];
int n,m;

bool find(int t1){
    for(int i=1;i<=m;i++){
        if(g[t1][i]&&vist[i]==0){//存在关系且,没有被访问过
            vist[i] = 1; //标记已经访问过的节点
            if(pre[i]==0||find(pre[i])){ //i节点没有访问过,或者i节点的pre节点能腾出位置
                pre[i] = t1;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    //int n,m;
    int x;
    while(~scanf("%d%d",&n,&m)){
        scanf("%d",&x);
        int t1,t2;
        memset(g,0,sizeof(g));
        for(int i=0;i<x;i++){
            scanf("%d%d",&t1,&t2);
            g[t1][t2] = 1;
        }
        memset(pre,0,sizeof(pre));

        int ct = 0;
        
        for(int i=1;i<=n;i++){
            memset(vist,0,sizeof(vist));
            if(find(i))ct++;
        }
        cout<<ct<<endl;
    }

    return 0;
}

/*
4 4
7
1 1
1 2
2 2
2 3
3 1
3 2
4 3

*/
View Code

 

posted @ 2016-04-17 11:50  iEdson  阅读(158)  评论(0编辑  收藏  举报