hdu5090(二分图,建图技巧)

202 篇文章 0 订阅
123 篇文章 0 订阅

Game with Pearls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2205    Accepted Submission(s): 806


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

题意:有n个放珍珠的管子,tom在初始的时候放了一些珍珠在一些管子里面,现在jerry要向一些管子里面加0个或者k的倍数个珍珠在里面,如果最终所有的管子每个管子的装珍珠的数目能形成1,2,3,4,5,6,……,n-1, n 的话,就代表jerry胜利,否则tom胜利。

思路:刚开始以为是按照顺序的直接判断了结果wa了,所以说明顺序是无序的,然后考虑二分图,如何建图是关键。首先是我们的初态是有n个管子,要达到的末态是第i个管子里面有i个珍珠,所以我们可以枚举每个管子从最初始的状态能到达的所有末状态(上限是n),然后连边,直接跑二分图匹配的匈牙利算法就ok。

我的博客有二分图的模板:http://blog.csdn.net/martinue/article/details/51548708


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int N=555;///两边的最大数量
bool tu[N][N];
int from[N];///记录右边的点如果配对好了它来自哪里
bool use[N];///记录右边的点是否已经完成了配对
int n,m;///m,n分别表示两边的各自数量,n是左边,m是右边
bool dfs(int x)
{
    for(int i=1; i<=m; i++) ///m是右边,所以这里上界是m
        if(!use[i]&&tu[x][i])
        {
            use[i]=1;
            if(from[i]==-1||dfs(from[i]))
            {
                from[i]=x;
                return 1;
            }
        }
    return 0;
}
int hungary()
{
    int tot=0;
    memset(from,-1,sizeof(from));
    for(int i=1; i<=n; i++) ///n是左边,所以这里上界是n
    {
        memset(use,0,sizeof(use));
        if(dfs(i))
            tot++;
    }
    return tot;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(tu,0,sizeof(tu));
        int x,k;
        scanf("%d%d",&x,&k);
        for(int i=1; i<=x; i++)
        {
            int tt,num=0;
            scanf("%d",&tt);
            while(tt+num*k<=x)
                tu[i][tt+num++*k]=1;
        }
        n=m=x;
        int ans=hungary();
        if(ans<x)
            puts("Tom");
        else
            puts("Jerry");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值