hdu 5090 Game with Pearls(最大匹配)

Game with Pearls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 196    Accepted Submission(s): 120

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 

题意:Jerry 和 Tom 玩一个游戏 , 给你 n 个盒子 , a[ i ] 表示开始时 ,第 i 个盒子中的小球的个数 。 然后 Jerry 可以在每个

盒子里加入 0 或 k的倍数的小球 , 操作完后,Jerry 可以重新排列 盒子的顺序,最终使 第 i 个盒子中有 i 个小球。 若Jerry能

使最终的盒子变成那样,就输出 “Jerry” ,否则 输出 “Tom” 。

思路: 最大匹配,以样例二为例 :

然后求出最大匹配数cnt ,若 cnt = = n ,输出 “Jerry” ,否则 输出 “Tom” 。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 110 ;

bool con[maxn][maxn],vis[maxn];
int match[maxn],n,k,x;

void initial()
{
    memset(con,0,sizeof(con));
    memset(match,-1,sizeof(match));
}

void input()
{
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++)
    {
         scanf("%d",&x);
         while(x<=n)
         {
              con[x][i]=1;
              x+=k;
         }
    }
}

bool dfs(int x)
{
    for(int i=1;i<=n;i++)
    {
        if(!vis[i] && con[x][i])
        {
            vis[i]=1;
            if(match[i]==-1 || dfs(match[i]))
            {
                 match[i]=x;
                 return true;
            }
        }
    }
    return false;
}

void solve()
{
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
         memset(vis,0,sizeof(vis));
         if(dfs(i))  cnt++;
    }
    if(cnt==n)   printf("Jerry\n");
    else   printf("Tom\n");
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        initial();
        input();
        solve();
    }
    return 0;
}
时间: 2024-08-01 09:04:56

hdu 5090 Game with Pearls(最大匹配)的相关文章

hdu 5090 Game with Pearls (额,, 想法题吧 / 二分图最大匹配也可做)

题意: 给你N个数,a1,,,,an.代表第i个管子里有ai个珍珠. 规定只能往每根管里增加k的倍数个珍珠. 如果存在一套操作,操作完毕后可以得到1~N的一个排列,则Jerry赢,否则Tom赢. 问谁赢. 思路: 将a1...an从小到大排序,可知道每根管里的数只能增不能减.将最后的1...N中的每个数一定是由小于等于它的数加上若干个K得到来的. 额..直接看代码吧 代码: int a[1005]; int m,n,k; int main(){ //freopen("test.in",

HDU 5090 Game with Pearls(二分匹配)

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

[HDU 5090] Game with Pearls (贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 题目大意:给你n个数,问你给若干个数增加c*k(c>=0)能否组成1,2,3,4,5,...,n? 今天下午作比赛的时候,我先用了个dfs,看这个a[i]匹配的是1到n的哪个数. 后来TLE到死... 仔细想想,首先,如果这个数是小的数的话,那么它可以匹配很多种,因此如果先将它匹配掉了会浪费,因为它“能力”大. 因此就可以排个序,从大到小进行匹配. 我也不知道怎么用数学语言去证明... 1 #

hdu 5090 Game with Pearls 同余类

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 题目大意是有n个数 给一个k 每次你只能给其中一个数加上“k的非负整数倍的值”(包括0) 问最终能否让这堆数变成从1到n各有一个 简单排序完贪心是不对的 正确的思想是同余类 因为实质上每次只能加k,所以模k同余的可归为一类 然后分别对每一个同余类是否有可能合法 不合法的情形有两种 a.该同余类内的数不够多或太多 b.该同余类内的对应位置的数不够小 如果按这种想法走还需特判一下模k得0的情况 总

HDU 5090 Game with Pearls

直接贪心就可以了,接收的时候把0都加上k,每次从最小的数开始找,如果有多个相同的,那么保留一个,其他的都加上k #include<stdio.h> #include<algorithm> using namespace std; int a[105]; int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif int T,n,k; scanf("%d&q

贪心 HDOJ 5090 Game with Pearls

题目传送门 1 /* 2 题意:给n, k,然后允许给某一个数加上k的正整数倍,当然可以不加, 3 问你是否可以把这n个数变成1,2,3,...,n, 可以就输出Jerry, 否则输出Tom. 4 贪心:保存可能变成的值的方案数,当一个符合,其他所有可能方案减1 5 最大匹配 详细解释:http://blog.csdn.net/u012596172/article/details/40784773?utm_source=tuicool 6 */ 7 #include <cstdio> 8 #i

HDU 5090 二分最大匹配

Game with Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 646    Accepted Submission(s): 284 Problem Description Tom and Jerry are playing a game with tubes and pearls. The rule of the ga

HDU ACM 5090 Game with Pearls-&gt;二分图最大匹配或?

题意:Jerry.Tom玩游戏,给你出n个盒子,a[ i ]表示初始时,第i个中小球个数.之后Jerry可在每个盒子中加入0或k倍的小球,完成后,Jerry可以重排盒子的顺序,若能使第i个盒子中有i个小球,则Jerry获胜,输出"Jerry",否则输出"Tom" . 分析:首先统计每种数量的球有多少个盒子,然后从小到大分过去,剩下的盒子放到i+k的位置,这样扫描一遍数组,若有超过一个及一个以上数量的球不能找到对应的盒子则Tom赢,否则Jerry赢. #include

HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】

Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1045 Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, e