首页 > 代码库 > HDU5090 Game with Pearls

HDU5090 Game with Pearls

/*
HDU5090 Game with Pearls
http://acm.hdu.edu.cn/showproblem.php?pid=5090
匈牙利算法
 *
 */

#include<stdio.h>
#include<algorithm>
using namespace std;
const int Nmax=305;
int t,n,k,x,ans;
int match[Nmax],book[Nmax],map[Nmax][Nmax];
int init()
{
    ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            map[i][j]=0;
    for(int i=1;i<=n;i++)
        match[i]=book[i]=0;
}

int dfs(int u)
{
    for(int i=1;i<=n;i++)
    {
        if(map[i][u] && !book[i])
        {
            book[i]=1;
            if(!match[i] || dfs(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
        
    }
    return 0;
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            for(int j=x;j<=n;j+=k)
                map[j][i]=1;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++) book[j]=0;
            if(dfs(i))
                ans++;
        }
        if(ans==n)
            printf("Jerry\n");
        else
            printf("Tom\n");
    }
    return 0;
}

 

HDU5090 Game with Pearls