题干:

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 

解题报告:

将每个数和可以组成的数连边,建图。然后跑二分图匹配就可以了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 555 + 5;
int n,m,k;
bool line[2005][2005];
bool used[MAX];
int nxt[MAX],a[MAX];
bool find(int x) {for(int i = 1; i<=n; i++) {if(!used[i] && line[x][i]) {used[i] = 1;if(!nxt[i] || find(nxt[i])) {nxt[i] = x;return 1;}}}return 0 ;
}
int match() {int res = 0;for(int i = 1; i<=n; i++) {memset(used,0,sizeof used);if(find(i)) res++;}return res == n;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d%d",&n,&k);memset(nxt,0,sizeof nxt);memset(line,0,sizeof line);for(int i = 1; i<=n; i++) {scanf("%d",a+i);for(int j = 0; j*k + a[i] <= n; j++) {int now = j*k+a[i];line[i][now] = 1;}}if(match()) puts("Jerry");else puts("Tom");}return 0 ;
}

【HDU - 5090】Game with Pearls (匈牙利算法,二分图匹配)相关推荐

  1. 【HDU - 5943】Kingdom of Obsession(数论,素数间隔结论,构造,思维,匈牙利算法,匹配问题)

    题干: There is a kindom of obsession, so people in this kingdom do things very strictly. They name the ...

  2. 【模板】匈牙利算法 二分图最大匹配题模板

    [任务] 给定一个二分图,用匈牙利算法求这个二分图的最大匹配数. [说明] 求最大匹配,那么我们希望每一个在左边的点都尽量找到右边的一个点和它匹配. 我们一次枚举左边的点x的所有出边指向的点y, 若y ...

  3. 浅谈匈牙利算法(二分图最大匹配)

    前置知识 一张图是二分图,当且仅当它的点可以被分成两部分,而这张图上的所有边的两个端点,都分属不同的部分.我们称这两个点集,一个叫左部,一个叫右部.左部中的点叫左部点:右部中的点叫右部点. 一张图的一 ...

  4. NOI图论算法:二分图匹配

    二分图匹配 算法竞赛入门经典训练指南+陈锋+ch5.5_二分图的匹配 https://www.bilibili.com/video/BV1j5411x7PU SWPU-ACM每周算法讲堂-匈牙利算法 ...

  5. HDU 2389 Rain on your Parade (二分图匹配)

    题意:天马上就要下雨了,然后有n个人,m把伞,然后分别给出人的坐标和他们跑的速度,以及伞的坐标,然后问在t时间内,最多能有多少人拿到伞. 题解:二分图匹配 之前做过一道类似的题目,是用最大流做的,但这 ...

  6. HDU - 5090 Game with Pearls(二分图最大匹配)

    题目链接:点击查看 题目大意:杰瑞和汤姆在玩游戏,游戏规则是:汤姆会拿来N个容器,每个容器内一开始就会包含1~N个小球,现在轮到杰瑞操作了,杰瑞可以选择往每个容器里加入0个或者任意k的倍数个小球,问杰 ...

  7. 【HDU - 1281 】棋盘游戏 (经典的二分图匹配,匈牙利算法,枚举删除顶点,必须边,关建边)

    题干: 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的"车",并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才 ...

  8. POJ-3041 匈牙利算法 二分图最大匹配

    踢以 给出多个点的坐标 有一种攻击 可以把一次干掉同一列的 或者干掉同一行的 求最少的攻击次数 肥西 由于问题是问选取最少的行和列干掉所有的陨石 可以把输入的r和c看成r和c之间有一条连边因为要实现干 ...

  9. HDU 5090 Game with Pearls (贪心)

    一道贪心的题,因为最小的不能由别的转化,所以每次贪心找最小的,其余的转化成大的. 从小到大,最小的如果不存在那么就break,否则减去一个,剩下的加k继续判断. #include<cstdio& ...

  10. 【Step1】【二分图匹配】poj 1274-The Perfect Stall

    题目链接 题目大意 输入数据中,第一行给出n,表示n个奶牛. 接下来n行,每行一个x,xi表示第i头奶牛可以选择x个谷仓中的一个进行匹配.接下来x个数,表示谷仓的编号(1~n之间) 一个谷仓也只能有一 ...

最新文章

  1. QT+VS打包发布流程该怎么做?
  2. Microbiome: 16S rRNA基因拷贝数应该被校正吗?
  3. Oracle cursor_sharing 参数 详解
  4. 阿里3篇技术论文入选国际顶级会议FAST2020,全球第一!
  5. c++interesting转换为uint_巴西插头标准,旅行转换插头是怎样的?
  6. atitit.浏览器web gui操作类库 和 操作chrome浏览器的类库使用总结
  7. journalctl清空日志
  8. Google退出中国的三大原因和四大后果(转载)
  9. 解决小程序图片上传问题
  10. three.js特效
  11. matlab瓶盖盖严检查,口服液瓶盖密封性的质量检测方法你知道吗?
  12. java 时间处理工具类
  13. 显示HTML的版权符号
  14. 2021年山东省安全员C证试题及解析及山东省安全员C证模拟考试
  15. 网络安全——Webshell管理工具
  16. 1+X 云计算平台运维与开发(初级)
  17. [NAS] Synology (群晖) DSM7.0 使用自定义供应商DDNS
  18. python+adb 控制安卓手机拍照并传电脑
  19. Echarts动态数据折线图
  20. 看不懂别做爬虫-----python爬虫实战---大众点评评论

热门文章

  1. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第25篇]使用特殊的素数定义GF(p)和GF(2^n)的方法
  2. [Leedcode][JAVA][第560题][和为K的子数组][Hashmap][数组]
  3. LinuX 硬盘分区细节详谈 【 整理至 LinuxSir BY FreeXploiT 】
  4. B. Trouble Sort Codeforces Round #648 (Div. 2)
  5. 1251: [蓝桥杯2015初赛]星系炸弹
  6. DOM Element对象的offsetXXX方法
  7. Java设计模式笔记(6)观察者模式
  8. 51单片机流水灯用c语言,51单片机之流水灯(C语言和汇编两个版本)
  9. 全局莫兰指数_空间自相关 | 莫兰指数
  10. html5 支持php标签吗,HTML5新增标签使用方法