zoukankan      html  css  js  c++  java
  • Problem: [Usaco2018 Open]Family Tree

    Problem: [Usaco2018 Open]Family Tree

    Time Limit: 1 Sec Memory Limit: 128 MB

    Description

    Farmer John拥有一个传承数代的家族经营的农场,其中的奶牛的根源同样也可以在这个农场中追溯数代。通过检索古老的记录,Farmer John好奇于现在的这群奶牛互相之间是什么关系。请帮帮他!

    Input

    输入的第一行包含NN(1≤N≤100),之后是两头奶牛的名字。
    每头奶牛的名字都是由至多10个大写字母(A…Z)组成的字符串。Farmer John好奇于这行输入中这两头奶牛之间的关系。
    接下来的NN行,每行包含两头奶牛的名字X和Y,表示X是Y的母亲。

    Output

    输出包含一行,表示输入第一行指定的两头奶牛之间的关系
    (简单起见,在下面的例子中,将这两头奶牛称为BESSIE和ELSIE)。
    下面是可能出现的不同种类的关系:
    如果BESSIE和ELSIE的母亲是同一头奶牛,输出“SIBLINGS”。
    BESSIE可能是ELSIE的直系后代,也就是说ELSIE是BESSIE的母亲(mother),
    外祖母(grand-mother),外曾外祖母(great-grand-mother),外曾外曾外祖母(great-great-grand-mother),等等。
    如果是这种情况,输出“ELSIE is the (relation) of BESSIE",其中(relation)是适当的关系,
    比如“great-great-grand-mother”。
    如果ELSIE不是BESSIE的某个祖先或姐妹,但是是BESSIE的某个祖先的孩子,
    那么ELSIE就是BESSIE的姨母(aunt)。
    (译者注:英语原题在这里表述有误,供题人已作出声明。)
    如果ELSIE是BESSIE的外祖母的孩子,输出“ELSIE is the aunt of BESSIE”;
    如果ELSIE是BESSIE的外曾外祖母的孩子,输出“ELSIE is the great-aunt of BESSIE”;
    如果ELSIE是BESSIE的外曾外曾外祖母的孩子,输出“ELSIE is the great-great-aunt of BESSIE”;以此类推。
    如果BESSIE和ELSIE有任何其他的亲戚关系(也就是说,她们有共同的祖先),她们就是表姐妹,输出“COUSINS”。
    如果BESSIE和ELSIE既没有共同的祖先,其中任何一头也不是另一头的直系后代,输出“NOT RELATED”。
    下图描述了上述关系,你只需考虑这些关系。
    观察到有一些像是“甥女(niece)”(姊妹的女儿)的关系是不必要的,这是由于如果BESSIE是ELSIE的甥女,那么ELSIE就是BESSIE的姨母。
    在这里插入图片描述

    Sample Input

    7 AA BB
    MOTHER AA
    GGMOTHER BB
    MOTHER SISTER
    GMOTHER MOTHER
    GMOTHER AUNT
    AUNT COUSIN
    GGMOTHER GMOTHER

    Sample Output

    BB is the great-aunt of AA
    代码如下

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <cassert>
    using namespace std;
    #define MAXN 100
      
    int N;
    string daughter[MAXN];
    string mother[MAXN];
      
    // returns mother of cow, or "" if mother does not exist
    string find_mother(string cow)
    {
        for(int i=0;i<N;i++)
            if(cow == daughter[i])
                return mother[i];
        return "";
    }
      
    // returns number of generations by which cow1 is removed from cow2
    // if cow1 is a direct ancestor of cow2.
    // returns -1 otherwise.
    int is_ancestor(string cow1, string cow2)
    {
        int counter = 0;
        while(cow2 != "")
        {
            if(cow1 == cow2)
                return counter;
            cow2 = find_mother(cow2);
            counter++;
        }
        return -1;
    }
      
    int main()
    {
        string bessie, elsie;
        cin >> N >> bessie >> elsie;
        for(int i=0;i<N;i++)
            cin >> mother[i] >> daughter[i];
         
        string cow = bessie;
        int b = 0;
        while(cow != "")
        {
            if(is_ancestor(cow, elsie) != -1)
                break;
            cow = find_mother(cow);
            b++;
        }
        if(cow == "")
        {
            cout << "NOT RELATED
    ";
            return 0;
        }
        int a = is_ancestor(cow, elsie);
        if(a == 1 && b == 1) cout << "SIBLINGS
    ";
        else if(a > 1 && b > 1) cout << "COUSINS
    ";
        else
        {
            if(a > b) swap(elsie, bessie), swap(a, b);
            cout << elsie << " is the ";
            for(int i=0;i<b-2;i++) cout << "great-";
            if(b > 1 && a == 0) cout << "grand-";
            if(a == 0) cout << "mother";
            else cout << "aunt";
            cout << " of " << bessie << '
    ';
        }
        return 0;
    }
    
  • 相关阅读:
    python3
    python2
    python的爬虫
    SQL SEVERE 基本用法 1
    安装SQL SEVER 2017 express 轻量入门级软件 安装教程
    面试学习资料
    后端架构师--总结网址收藏(个人)
    JVM学习网址(收集总结)
    RabbitMQ--学习资源汇
    Redis 学习资料目录(Important)
  • 原文地址:https://www.cnblogs.com/ZhaoChongyan/p/11740442.html
Copyright © 2011-2022 走看看