HDU 5090 Game with Pearls(二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090

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
 
Recommend

题意:

有 n 个容器,每一个里面有一些珍珠。

能够在随意容器中加入 k 的倍数个珍珠。

问终于能否使得每一个容器分别有1 ~ n颗珍珠。

代码例如以下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAXN 177
int N;
int g[MAXN][MAXN], linker[MAXN];
bool used[MAXN];
int dfs(int L)//从左边開始找增广路径
{
int R;
for(R = 1 ; R <= N ; R++)//这个顶点编号从0開始。若要从1開始须要改动
{
if(g[L][R]!=0 && !used[R])
{
//找增广路。反向
used[R]=true;
if(linker[R] == -1 || dfs(linker[R]))
{
linker[R]=L;
return 1;
}
}
}
return 0;//这个不要忘了。常常忘记这句
}
int hungary()
{
int res = 0 ;
memset(linker,-1,sizeof(linker));
for(int L = 1; L <= N; L++)
{
memset(used,0,sizeof(used));
if(dfs(L))
res++;
}
return res;
}
int main()
{
int t;
int k, res, tt;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&N,&k);
memset(g,0,sizeof(g));
for(int i = 1 ; i <= N ; i++ )
{
scanf("%d",&tt);
while(tt <= N)
{
g[tt][i] = 1;
tt+=k;
}
}
res = hungary();
if(res == N)
{
printf("Jerry\n");
}
else
{
printf("Tom\n");
}
}
return 0 ;
}
上一篇:使用表达式避免拼接SQL语句


下一篇:hdu 5090 Game with Pearls (额,, 想法题吧 / 二分图最大匹配也可做)