欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

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

发布时间:2023/12/10 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【HDU - 5090】Game with Pearls (匈牙利算法,二分图匹配) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题干:

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 (匈牙利算法,二分图匹配)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。