hdu 2063 匈牙利算法

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<map>
#include<queue>
#include <deque>
#include <list>
#include <ctime>
#include <stack>
#include <vector>
#include<iomanip>
#include<set>
#include <bitset>
#include <cassert>
using namespace std;
#define Maxn 512
#define mod
typedef long long ll;
typedef pair<int, int> PII;
#define FOR(i,j,n) for(int i=j;i<=n;i++)
#define DFR(i,j,k) for(int i=j;i>=k;--i)
#define lowbit(a) a&-a
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem(a) memset(a,0,sizeof(a))
#define eps 1e-9
#define PB push_back
#define MP make_pair
#define AA first
#define BB second
#define SZ size()
#define BG begin()
#define OP begin()
#define ED end()
#define SQ(x) ((x)*(x))
const int inf = 0x7f7f7f7f;
const double pi = acos(-1.0);
int k,m,n,sum;
int e[Maxn][Maxn];
int match[Maxn];
int book[Maxn];
int DFS(int u)
{   FOR(i,1,m)
    {   if(!book[i] && e[u][i])
        {
        book[i]=1;
        if(!match[i]  ||  DFS(match[i]))
        {   match[i]=u;
            //match[u]=i;
            return 1;
        }
        }
    }
    return 0;
}
int main()
{   while(~scanf("%d",&k) && k)
    { scanf("%d%d",&n,&m);
      mem(e);
      FOR(i,1,k)
     {int a,b;scanf("%d%d",&a,&b);e[a][b]=1;}
      sum=0;
      mem(match);
      FOR(i,1,n)
      {   mem(book);
          if(DFS(i))sum++;
      }
      printf("%d\n",sum);
    }
    return 0;
}



你可能感兴趣的:(匈牙利算法)