HDOJ 题目5090 Game with Pearls(二分图)

Game with Pearls

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

Total Submission(s): 919    Accepted Submission(s): 353

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:  5181 5180 5179 5178 5177

ac代码

#include<stdio.h>
#include<string.h>
int link[1010];
int map[1010][1010],v[1010];
int n;
int dfs(int x)
{
	int i;
	for(i=1;i<=n;i++)
	{
		if(map[x][i]==!v[i])
		{
			v[i]=1;
			if(link[i]==-1||dfs(link[i]))
			{
				link[i]=x;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int k,i;
		scanf("%d%d",&n,&k);
		memset(map,0,sizeof(map));
		for(i=1;i<=n;i++)
		{
			int num;
			scanf("%d",&num);
			while(num<=n)
			{
				map[num][i]=1;
				num+=k;
			}
		}
		memset(link,-1,sizeof(link));
		int sum=0;
		for(i=1;i<=n;i++)
		{
			memset(v,0,sizeof(v));
			if(dfs(i))
				sum++;
		}
		if(sum==n)
		{
			printf("Jerry\n");
		}
		else
			printf("Tom\n");
	}
}
时间: 2024-08-29 18:36:19

HDOJ 题目5090 Game with Pearls(二分图)的相关文章

HDOJ 5090 Game with Pearls 二分图匹配

简单的二分图匹配: 每一个位置的数可能边成那些数连边即可 Game with Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 122    Accepted Submission(s): 85 Problem Description Tom and Jerry are playing a game with tubes a

贪心 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

HDOJ题目3729 I&#39;m Telling the Truth(二分图)

I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1629    Accepted Submission(s): 805 Problem Description After this year's college-entrance exam, the teacher did a survey i

HDOJ 题目分类

HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:    用STL中的next_permutation()1029:1032:1037:1039:1040:1056:1064:1065:1076:    闰年 1084:1085:1089,1090,1091,1092,1093,1094, 1095, 1096:全是A+B1108:1157:1196:1

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

HDOJ 题目4738 Caocao&#39;s Bridges(双联通,求桥)

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1752    Accepted Submission(s): 642 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. B

HDOJ 题目4349 Xiao Ming&#39;s Hope(找规律)

Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 1015 Problem Description Xiao Ming likes counting numbers very much, especially he is fond of co

HDOJ 题目1528 Card Game Cheater(二分图最小点覆盖)

Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1357    Accepted Submission(s): 722 Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rul

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",