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 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上海全国邀请赛——题目重现(感谢上海大学提供题目)

题目意思:

给一序列,序列每个数字可以加上0个或多个k,若这个序列能形成每个i有a[i]==i,输出jerry,否则输出tom。

思路:

原序列中若a变成c,那么b就不能变成c了,这样就满足二分匹配了。a可以变成a+j*k(j>=0&&j<=n) ,a和a+j*k连边建图求二分最大匹配,若匹配数ans==n输出jerry,否则输出tom。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 using namespace std;
 8
 9 #define N 105
10
11 int max(int x,int y){return x>y?x:y;}
12 int min(int x,int y){return x<y?x:y;}
13 int abs(int x,int y){return x<0?-x:x;}
14
15 int a[N];
16 int n, m, k;
17 int from[N];
18 bool visited[N];
19 vector<int>ve[N];
20
21 int march(int u){
22     int i, v;
23     for(i=0;i<ve[u].size();i++){
24         v=ve[u][i];
25         if(!visited[v]){
26             visited[v]=true;
27             if(from[v]==-1||march(from[v])){
28                 from[v]=u;
29                 return 1;
30             }
31         }
32     }
33     return 0;
34 }
35
36 main()
37 {
38     int i, j;
39     cin>>m;
40     while(m--){
41         scanf("%d %d",&n,&k);
42         for(i=1;i<=n;i++) scanf("%d",&a[i]);
43         for(i=0;i<=n;i++) ve[i].clear();
44         bool flag;
45         for(i=1;i<=n;i++){
46             flag=false;
47             for(j=0;j<=i;j++){
48                 if(a[i]+j*k<=n&&(a[i]+j*k)) {
49                     ve[i].push_back(a[i]+j*k);
50                 }
51                 else break;
52             }
53         }
54
55         memset(from,-1,sizeof(from));
56         int ans=0;
57         for(i=1;i<=n;i++){
58             memset(visited,false,sizeof(visited));
59             if(march(i)) ans++;
60         }
61         if(ans!=n) printf("Tom\n");
62         else printf("Jerry\n");
63     }
64 }
时间: 2024-08-12 16:29:51

HDU 5090 二分最大匹配的相关文章

HDU 5093 二分最大匹配

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 394    Accepted Submission(s): 178 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible o

HDU 5090 二分匹配

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

HDU 1498 50 years, 50 colors(二分最大匹配之最小点覆盖)

题目地址:HDU 1498 晕啊...三个人同时做的这个题,结果全都理解错意思了..而且每个人理解错的地方还都不一样..但是样例还都能过,...简直炫酷... 题意:n*n的矩阵放置不同的颜色(不同的数字代表不同的颜色),你有k次选择,每一次只能选择某一行或某一列,可以消除该行(列)的所有颜色,问有哪几种颜色,无论怎样经过k次选择后依然无法完全抹去. 这个题的思路就是分别求出每种颜色的最少操作次数.然后只要大于k次的就是不符合要求的.然后输出就行了. #include <iostream> #

Hdu 2389 二分匹配

题目链接 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 2644    Accepted Submission(s): 823 Problem Description You’re giving a party in the garden of your villa by the sea. T

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))

题目链接: http://poj.org/problem?id=3041 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently

HDU 1528 (二分图最大匹配 + 最小覆盖, 14.07.17)

Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the c

hdu2063 匈牙利算法 二分最大匹配模版题

过山车 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐.但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或P

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)