【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 5Sample 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 (匈牙利算法,二分图匹配)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: smsss.exe - smsss是什么
- 下一篇: 申请信用卡没有固定电话怎么办 不填固定电