日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 (匈牙利算法,二分图匹配)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。