【最小顶点覆盖】POJ 3041

最小顶点覆盖=最大匹配数,以行为x集合,列为y集合

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#define LL long long
#define MIN -99999999
#define MAX 99999999
#define pii pair<int ,int>

#define bug cout<<"here!!"<<endl
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout)
#define eps 1e-8
#define N 505
int g[N][N];
bool vis[N];
int mat[N];
int n;
bool sear(int s){
    int i,j;
    for(i=1;i<=n;i++){
        if(g[s][i] && !vis[i]){
            vis[i]=1;
            if(mat[i]==0 || sear(mat[i])){
                mat[i] = s;
                return true;
            }
        }
    }
    return false;
}
int main(){
    int m;
    int i,j;
    while(scanf("%d%d",&n,&m) !=-1){
        memset(g,0,sizeof(g));
        while(m--){
            int x,y;
            scanf("%d%d",&x,&y);
            g[x][y] = 1;
        }
        int cnt=0;
        memset(mat,0,sizeof(mat));
        for(i=1;i<=n;i++){
            memset(vis,0,sizeof(vis));
            if(sear(i))cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}


你可能感兴趣的:(【最小顶点覆盖】POJ 3041)