`
Simone_chou
  • 浏览: 192571 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Game with Pearls(二分图)

    博客分类:
  • HDOJ
 
阅读更多

Game with Pearls

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


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

 

     题意:

     给出 T 组样例,后给出 N 和 K,代表有 N 条管子,后给出 N 条管子上面的初始状态珍珠数。可以向任意一条管子添加 K 的珍珠数,问能否使管子分别出现 1 ~ N 的数量。

 

     思路:

     二分图,N 最大值才100,所以建图也是非常方便的。将每个数 ans 加 K 循环至 ans > N 来建图,最后判断二分匹配后是否满配即可。

 

     AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 105;

int n, k;
int num[MAX];

int G[MAX][MAX];
int linker[MAX];
bool vis[MAX];

void build () {
    memset(G, 0, sizeof(G));

    for (int i = 1; i <= n; ++i) {
        for (int j = num[i]; j <= n; j += k) {
            G[i][j] = 1;
        }
    }
}

bool dfs (int x) {
    for (int i = 1; i <= n; ++i) {
        if (G[x][i] && !vis[i]) {
            vis[i] = 1;
            if (linker[i] == -1 || dfs(linker[i])) {
                linker[i] = x;
                return true;
            }
        }
    }

    return false;
}

int hungary () {
    int res = 0;
    memset(linker, -1, sizeof(linker));

    for (int i = 1; i <= n; ++i) {
        memset(vis, 0, sizeof(vis));
        if (dfs(i)) ++res;
    }

    return res;
}

int main () {

    int t;
    scanf("%d", &t);

    while (t--) {

        scanf("%d%d", &n, &k);

        for (int i = 1; i <= n; ++i) {
            scanf("%d", &num[i]);
        }

        build();

        if (n == hungary()) printf("Jerry\n");
        else printf("Tom\n");

    }

    return 0;
}

 

 

分享到:
评论

相关推荐

    Programming Pearls 2Nd Edition

    例如,书中讨论了二分查找、排序算法、图算法等,并分析了它们的时间复杂度和空间复杂度。 3. **编程风格与代码质量**:书中提倡编写清晰、可读性强的代码,讨论了代码重构和模块化设计的重要性,以及如何避免常见...

    Programming Pearls

    《Programming Pearls》是一本由著名计算机科学家Jon Bentley撰写的经典著作,本书于2000年由Addison-Wesley出版社出版(ISBN 0-201-65788-0)。这本书不仅仅是一本关于编程技巧的教程,更是一部探讨编程艺术、工程...

    more progamming pearls,编程珠玑续

    《more progamming pearls,编程珠玑续》是继《progamming pearls,编程珠玑》的后续书籍,只是有的地方不是很清晰。

    Programming Pearls 2nd edition

    Programming Pearls 2nd edition Paperback: 256 pages Publisher: Addison-Wesley Professional; 2 edition (October 7, 1999) Language: English ISBN-10: 0201657880 ISBN-13: 978-0201657883

    Programming Pearls(2nd) epub

    Programming Pearls(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    Programming Pearls 编程珠玑 英文版 文字版

    4. **数据结构**:深入探讨了数组、链表、树、图等数据结构的特性,以及如何根据问题选择合适的数据结构。 5. **错误处理和调试**:讲解了如何有效地定位和修复程序错误,以及编写健壮代码的重要性。 6. **代码...

    编程珠玑II(More programming Pearls) pdf

    编程珠玑II(More programming Pearls) pdf编程珠玑II(More programming Pearls) pdf

    .High.Performance.Parallelism.Pearls.0128021187

    High Performance Parallelism Pearls shows how to leverage parallelism on processors and coprocessors with the same programming – illustrating the most effective ways to better tap the computational ...

    Bentley J. More programming pearls

    More programming pearls

    编程珠玑 programing pearls

    本书给出了一些精心设计的有趣而且颇具指导意义的程序,书中充满了对实用程序设计技巧及基本设计原则的清晰而机智的描述。《编程珠玑》(第2版)(英文版)增加了3个方面的新内容:测试、调试和计时;...

    编程珠玑2 More Programming Pearls

    - **数据结构选择**:书中详细讨论了不同数据结构的特点及其适用场景,帮助读者理解何时应使用数组、链表、树、图等结构,并给出了实例代码。 - **算法设计与优化**:通过对常见算法(如排序、搜索)的深入分析,...

    More+Programming+Pearls中英文以及source code for 1

    More+Programming+Pearls以及source code for 1

    programming pearls 文章合集

    编程珠玑 作者20年前(从1983年到1987年)在communications of the ACM 上连载发表的30篇文章,比《编程珠玑》书写的要详细。很启发人的。

    POJ1260-Pearls

    【标题】"POJ1260-Pearls" 是北京大学在线编程平台POJ上的一道题目,这道题目主要考察的是算法设计和实现能力。POJ(Problem Online Judge)是一个面向全球程序员的在线编程练习系统,它提供了大量的编程题目供用户...

    Programing Pearls, Second Edition

    《编程珠玑》第二版是计算机科学领域内一部备受推崇的经典著作,由知名计算机科学家乔恩·本特利(Jon Bentley)撰写。本书不仅是一部关于软件工程中编程艺术的瑰宝,更是对程序设计技巧、算法创新以及代码优化等...

    北大POJ1260-Pearls

    北大POJ1260-Pearls

    Programming Pearls (非删减版)

    之前下载了“COMPLETE”版,但实际是样书。这个是完全版。Enjoy it!

Global site tag (gtag.js) - Google Analytics