Game with Pearls(二分図)
3452 ワード
Game with Pearls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 145 Accepted Submission(s): 96
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
タイトル:
T組のサンプルを与え,NとKを後に与え,N本の管を表し,後にN本の管の上の初期状態の真珠数を与えた.いずれかのチューブにKの真珠数を加えて、チューブにそれぞれ1~Nの数を出すことができるかを聞くことができます.
考え方:
二分図、Nの最大値は100なので、図を建てるのもとても便利です.各数ansにKを加えてans>Nにループして図を作成し,最後に二分整合後に満配であるか否かを判断すればよい.
AC:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 145 Accepted Submission(s): 96
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
タイトル:
T組のサンプルを与え,NとKを後に与え,N本の管を表し,後にN本の管の上の初期状態の真珠数を与えた.いずれかのチューブにKの真珠数を加えて、チューブにそれぞれ1~Nの数を出すことができるかを聞くことができます.
考え方:
二分図、Nの最大値は100なので、図を建てるのもとても便利です.各数ansにKを加えてans>Nにループして図を作成し,最後に二分整合後に満配であるか否かを判断すればよい.
AC:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = 105;
int n, k;
int num[MAX];
int G[MAX][MAX];
int linker[MAX];
bool vis[MAX];
void build () {
memset(G, 0, sizeof(G));
for (int i = 1; i <= n; ++i) {
for (int j = num[i]; j <= n; j += k) {
G[i][j] = 1;
}
}
}
bool dfs (int x) {
for (int i = 1; i <= n; ++i) {
if (G[x][i] && !vis[i]) {
vis[i] = 1;
if (linker[i] == -1 || dfs(linker[i])) {
linker[i] = x;
return true;
}
}
}
return false;
}
int hungary () {
int res = 0;
memset(linker, -1, sizeof(linker));
for (int i = 1; i <= n; ++i) {
memset(vis, 0, sizeof(vis));
if (dfs(i)) ++res;
}
return res;
}
int main () {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &num[i]);
}
build();
if (n == hungary()) printf("Jerry
");
else printf("Tom
");
}
return 0;
}