hdu 1498 匈牙利算法

#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 128
#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 e[Maxn][Maxn];
int book[Maxn],match[Maxn],ans[Maxn],n,k;
int DFS(int u,int color)
{   FOR(i,1,n)
    {   if(e[u][i] == color && !book[i])
            {   book[i]=1;
            if(!match[i] || DFS(match[i],color))
            {   match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}
int getnum(int color)
{   mem(match);
    int sum=0;
    FOR(i,1,n)
    {   mem(book);
        if(DFS(i,color))sum++;
    }
    return sum;

}
int main()
{   while(~scanf("%d%d",&n,&k),n||k)
    {   mem(e);mem(ans);int  t=0;
        FOR(i,1,n)
        FOR(j,1,n)
        scanf("%d",&e[i][j]);
        FOR(i,1,50)
        {
            if(getnum(i)>k)
                ans[t++]=i;
        }
        if(!t)
            printf("-1\n");
        else
            {   FOR(i,1,t)
                printf("%d%c",ans[i],i==t?'\n':' ');
            }
    }
    return 0;
}

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