hdu5090 匈牙利演算法二分圖最大匹配問題

life4711發表於2014-11-03

http://acm.hdu.edu.cn/showproblem.php?pid=5090

Problem Description
Tom and Jerry are playing a game with tubes and pearls. The rule of the game is:

1) Tom and Jerry come up together with a number K. 

2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube is at least 1 and at most N. 

3) Jerry puts some more pearls into each tube. The number of pearls put into each tube has to be either 0 or a positive multiple of K. After that Jerry organizes these tubes in the order that the first tube has exact one pearl, the 2nd tube has exact 2 pearls, …, the Nth tube has exact N pearls.

4) If Jerry succeeds, he wins the game, otherwise Tom wins. 

Write a program to determine who wins the game according to a given N, K and initial number of pearls in each tube. If Tom wins the game, output “Tom”, otherwise, output “Jerry”.
 

Input
The first line contains an integer M (M<=500), then M games follow. For each game, the first line contains 2 integers, N and K (1 <= N <= 100, 1 <= K <= N), and the second line contains N integers presenting the number of pearls in each tube.
 

Output
For each game, output a line containing either “Tom” or “Jerry”.
 

Sample Input
2 5 1 1 2 3 4 5 6 2 1 2 3 4 5 5
 

Sample Output
Jerry Tom
/**
hdu5090二分圖的最大匹配
一開始我用的是遍歷寫的,知道必然是TLE還是試了試,賽後才知道是一個二分圖的題,無奈整了這麼久的圖論竟然看不出這是二分圖,真是愧對隊友啊==
解題思路:因為最後的數是無序的,我們把每一個a[i]與二分圖另一側的a[i],a[i]+k……知道a[i]+n*k>n為止的所有點連一條邊,
最後匈牙利一遍如果滿流就可以實現,否則就呵呵了。
*/
/* **************************************************************************
//二分圖匹配(匈牙利演算法的DFS實現)
//初始化:g[][]兩邊頂點的劃分情況
//建立g[i][j]表示i->j的有向邊就可以了,是左邊向右邊的匹配
//g沒有邊相連則初始化為0
//uN是匹配左邊的頂點數,vN是匹配右邊的頂點數
//呼叫:res=hungary();輸出最大匹配數
//優點:適用於稠密圖,DFS找增廣路,實現簡潔易於理解
//時間複雜度:O(VE)
//***************************************************************************/
//頂點編號從0開始的
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

const int MAXN=220;
int n,k;//u,v數目
int g[MAXN][MAXN];
int linker[MAXN],a[220];
bool used[MAXN];

bool dfs(int u)//從左邊開始找增廣路徑
{
    int v;
    for(v=1; v<=n; v++) //這個頂點編號從0開始,若要從1開始需要修改
        if(g[u][v]&&!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                //找增廣路,反向
                linker[v]=u;
                return true;
            }
        }
    return false;//這個不要忘了,經常忘記這句
}

int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=1; u<=n; u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}
//******************************************************************************/
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        memset(g,0,sizeof(g));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if((j-a[i])%k==0&&j>=a[i])
                    g[i][j]=1;
            }
        }
        if(hungary()==n)
            printf("Jerry\n");
        else
            printf("Tom\n");
    }
    return 0;
}

遍歷的寫法雖然TLE還是放這裡吧,費了我不少心血呢==

#include <stdio.h>
#include <string.h>
#include <iostream>'
using namespace std;
int n,k,sa[200],s[200][200],b[200],flag[200],cont;
void dfs(int x)
{
    if(x==n+1)
    {
        int tt=0;
        for(int i=1;i<=n;i++)
            if(flag[i]==1)
                tt++;
        if(tt==n)
            cont=1;
        return;
    }
    for(int i=0;i<=b[x];i++)
    {
        if(cont==1)
            return;
        flag[s[x][i]]=1;
        dfs(x+1);
        flag[s[x][i]]=0;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&sa[i]);
            s[i][b[i]]=sa[i];
            for(int j=1;j<=n;j++)
            {
                //printff("(%d %d)\n",b[i],s[i][b[i]]);
                if(s[i][b[i]]+k>n)
                    break;
                b[i]++;
                s[i][b[i]]=s[i][b[i]-1]+k;
            }
        }
        // for(int i=1;i<=n;i++)
        //       printff("%d ",b[i]);
        //     printff("\n");
        /*for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=b[i];j++)
                printff("%d ",s[i][j]);
            printff("\n");
        }*/
        cont=0;
        memset(flag,0,sizeof(flag));
        dfs(1);
        if(cont==0)
            printff("Tom\n");
        else
            printff("Jerry\n");
    }
    return 0;
}


相關文章