简单的二分图匹配:

每个位置可以边到这些数字甚至可以边

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 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
  1. 2
  2. 5 1
  3. 1 2 3 4 5
  4. 6 2
  5. 1 2 3 4 5 5
 
Sample Output
  1. Jerry
  2. Tom
 
Source
 

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int maxn=110;
  9.  
  10. int n,K;
  11.  
  12. struct Edge
  13. {
  14. int to,next;
  15. }edge[maxn*maxn];
  16.  
  17. int Adj[maxn],Size;
  18.  
  19. void init()
  20. {
  21. memset(Adj,-1,sizeof(Adj)); Size=0;
  22. }
  23.  
  24. void add_edge(int u,int v)
  25. {
  26. edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;
  27. }
  28.  
  29. int linker[maxn];
  30. bool used[maxn];
  31.  
  32. bool dfs(int u)
  33. {
  34. for(int i=Adj[u];~i;i=edge[i].next)
  35. {
  36. int v=edge[i].to;
  37. if(!used[v])
  38. {
  39. used[v]=true;
  40. if(linker[v]==-1||dfs(linker[v]))
  41. {
  42. linker[v]=u;
  43. return true;
  44. }
  45. }
  46. }
  47. return false;
  48. }
  49.  
  50. int hungary()
  51. {
  52. int res=0;
  53. memset(linker,-1,sizeof(linker));
  54. for(int u=1;u<=n;u++)
  55. {
  56. memset(used,false,sizeof(used));
  57. if(dfs(u)) res++;
  58. }
  59. return res;
  60. }
  61.  
  62. int a[maxn];
  63.  
  64. int main()
  65. {
  66. int T_T;
  67. scanf("%d",&T_T);
  68. while(T_T--)
  69. {
  70. scanf("%d%d",&n,&K);
  71. init();
  72. for(int i=1;i<=n;i++)
  73. {
  74. scanf("%d",a+i);
  75. for(int j=0;a[i]+j*K<=n;j++)
  76. {
  77. int v=a[i]+j*K;
  78. add_edge(i,v);
  79. }
  80. }
  81. int pp=hungary();
  82. //cout<<"pp: "<<pp<<endl;
  83. if(pp==n) puts("Jerry");
  84. else puts("Tom");
  85. }
  86. return 0;
  87. }

版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

HDOJ 5090 Game with Pearls 二分图匹配的更多相关文章

  1. 贪心 HDOJ 5090 Game with Pearls

    题目传送门 /* 题意:给n, k,然后允许给某一个数加上k的正整数倍,当然可以不加, 问你是否可以把这n个数变成1,2,3,...,n, 可以就输出Jerry, 否则输出Tom. 贪心:保存可能变成 ...

  2. HDU5090--Game with Pearls 二分图匹配 (匈牙利算法)

    题意:给N个容器,每个容器里有一定数目的珍珠,现在Jerry开始在管子上面再放一些珍珠,放上的珍珠数必须是K的倍数,可以不放.最后将容器排序,如果可以做到第i个容器上面有i个珍珠,则Jerry胜出,反 ...

  3. HDOJ 5093 Battle ships 二分图匹配

    二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...

  4. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  5. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  6. BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2375  Solved: 1005[Submit][Sta ...

  7. HDU1281-棋盘游戏-二分图匹配

    先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...

  8. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

  9. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

随机推荐

  1. 源码笔记---MBProgressHUD

    前言 作为初学者,想要快速提高自己的水平,阅读一些优秀的第三方源代码是一个非常好的途径.通过看别人的代码,可以学习不一样的编程思路,了解一些没有接触过的类和方法. MBProgressHUD是一个非常 ...

  2. Ajax之旅(二)--XMLHttpRequest

         上文中提到的Ajax的异步更新.主要使用XMLHttpRequest对象来实现的,XMLHttpRequest对象能够在不向server提交整个页面的情况下,实现局部更新网页. 当页面所有载 ...

  3. Android之RecyclerView简单使用(三)

    使用过ListView滴小伙伴都知道.ListView有这样一个属性android:divider,用来设置每一个item之间切割线滴属性.问题来了,那么RecyclerView这个控件有没有这个属性 ...

  4. [Swift] Add Scroll View

    import UIKit class AboutViewController : UIViewController @IBOutlet weak var scrollView: UIScrollVie ...

  5. wordpress-nas

  6. Linux下kill进程脚本

    Linux下kill进程脚本 在Linux有时会遇到需要kill同一个程序的进程,然而这个程序有多个进程,一一列举很是繁琐,使用按名字检索,统一kill Perl脚本 使用方法 kill_all.pl ...

  7. 【C++竞赛 C】yyy的数学公式

    时间限制:1s 内存限制:32MB 问题描述 yyy遇到了一个数学问题如下: S_n=∑F(i) 其中F(i)表示i的最大奇因数 yyy的数学非常好,很快就得到了结果.现在他把问题交给你,你能解决吗? ...

  8. USB 3.0规范中译本第9章 设备框架

    本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 设备框架可以被分成三层: 最底层是总线接口层,传送和接收包. 中间层处理在总线接口和设备的各种端点之间路由数 ...

  9. HTML代码简写法:Emmet和Haml(转)

    HTML代码写起来很费事,因为它的标签多. 一种解决方法是采用模板, 在别人写好的骨架内,填入自己的内容.还有一种就是我今天想要介绍的方法----简写法. 常用的简写法,目前主要是Emmet和Haml ...

  10. Make chrome extension

    How to Make a Chrome Extension. https://robots.thoughtbot.com/how-to-make-a-chrome-extension Skip to ...