Hdoj 5090 game with pearls Bipartite Graph Matching

Source: Internet
Author: User


Simple Bipartite Graph Matching:

The number of edges at each position can be the number of connected edges.

Game with pearls Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 122 accepted submission (s): 85


Problem descriptiontom 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 ".
Inputthe 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.
Outputfor 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 
 
Source2014 Shanghai national invitational competition-reproduction of the questions (thanks to Shanghai University for providing the questions)


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=110;int n,K;struct Edge{    int to,next;}edge[maxn*maxn];int Adj[maxn],Size;void init(){    memset(Adj,-1,sizeof(Adj)); Size=0;}void add_edge(int u,int v){    edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;}int linker[maxn];bool used[maxn];bool dfs(int u){    for(int i=Adj[u];~i;i=edge[i].next)    {        int v=edge[i].to;        if(!used[v])        {            used[v]=true;            if(linker[v]==-1||dfs(linker[v]))            {                linker[v]=u;                return true;            }        }    }    return false;}int hungary(){    int res=0;    memset(linker,-1,sizeof(linker));    for(int u=1;u<=n;u++)    {        memset(used,false,sizeof(used));        if(dfs(u)) res++;    }    return res;}int a[maxn];int main(){    int T_T;    scanf("%d",&T_T);    while(T_T--)    {        scanf("%d%d",&n,&K);        init();        for(int i=1;i<=n;i++)        {            scanf("%d",a+i);            for(int j=0;a[i]+j*K<=n;j++)            {                int v=a[i]+j*K;                add_edge(i,v);            }        }        int pp=hungary();        //cout<<"pp: "<<pp<<endl;        if(pp==n) puts("Jerry");        else puts("Tom");    }    return 0;}



Hdoj 5090 game with pearls Bipartite Graph Matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.