POJ 3041(二分匹配

代码很简单,我是直接抄的模板,所谓的匈牙利算法其实就是最大流的特化版

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<utility>
#include<vector>
#include<cstring>
#include<cmath>
#define INF 0x7fffffff
#define pb push_back
#define pn(x) cerr<<x<<endl

using namespace std;
typedef long long ll;
const int maxv=500005;
int n,k;
int r[maxv],c[maxv];
vector<int> G[maxv];
int match[maxv];
bool used[maxv];
bool dfs(int v){
    used[v]=1;
    for(int i=0;i<G[v].size();i++){
        int u=G[v][i];
        int w=match[u];
        if(w<0||used[w]==0&&dfs(w)){
            match[u]=v;
            match[v]=u;
            return 1;
        }
    }
    return 0;
}
int bi_match(){
    memset(match,-1,sizeof match);
    int ans=0;
    for(int i=1;i<=2*n;i++){
        if(match[i]<0){
            memset(used,0,sizeof used);
        if(dfs(i)) ans++;
        }
    }
    return ans;
}
int main(){
    cin>>n>>k;
    for(int i=0;i<k;i++){/////row,1~n   col,n+1~2*n;
        scanf("%d%d",&r[i],&c[i]);
        G[r[i]].pb(c[i]+n);
        G[c[i]+n].pb(r[i]);
    }
    cout<<bi_match()<<endl;
    return 0;
}

 

posted @ 2015-05-09 17:46  PlusSeven  阅读(77)  评论(0)    收藏  举报