HDU 5090 Game with Pearls(二分匹配)

来源:互联网 发布:微软office for mac 编辑:程序博客网 时间:2024/06/07 02:41

题目链接: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
 

Source
2014上海全国邀请赛——题目重现(感谢上海大学提供题目)
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5098 5097 5096 5094 5093 

题意:

有 n 个容器,每个里面有一些珍珠。

可以在任意容器中添加 k 的倍数个珍珠。

问最终是否能使得每个容器分别有1 ~ n颗珍珠。


代码如下:

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;#define MAXN 177int N;int g[MAXN][MAXN], linker[MAXN];bool used[MAXN];int dfs(int L)//从左边开始找增广路径{    int R;    for(R = 1 ; R <= N ; R++)//这个顶点编号从0开始,若要从1开始需要修改    {        if(g[L][R]!=0 && !used[R])        {            //找增广路,反向            used[R]=true;            if(linker[R] == -1 || dfs(linker[R]))            {                linker[R]=L;                return 1;            }        }    }    return 0;//这个不要忘了,经常忘记这句}int hungary(){    int res = 0 ;    memset(linker,-1,sizeof(linker));    for(int L = 1; L <= N; L++)    {        memset(used,0,sizeof(used));        if(dfs(L))            res++;    }    return res;}int main(){    int t;    int k, res, tt;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&N,&k);        memset(g,0,sizeof(g));        for(int i = 1 ; i <= N ; i++ )        {            scanf("%d",&tt);            while(tt <= N)            {                g[tt][i] = 1;                tt+=k;            }        }        res = hungary();        if(res == N)        {            printf("Jerry\n");        }        else        {            printf("Tom\n");        }    }    return 0 ;}


2 0