uva 115

3 篇文章 0 订阅

#include<cstdio>

#include<iostream>

#include<string>

#include<cstring>

#include<map>

#include<utility>

#include<algorithm>

#define N 301

using namespace std;

typedef pair<int, int> Pair;

/*

 node[x] = x 的祖先

 注意 0 號為沒有祖先了

 */

int node[N] = {};

Pair getRelation(int first,int second)

{

    /*

     如果關係為 cousin:

     lev1,lev2 分別存共同祖先的各個順位

     如果某一方為另一方的 祖先:

     lev1 = 存他祖先的順位

     lev2 = -1

     反之則關係相反

     */

    int order1[N] = {}, order2[N];//[x] = x 的順位

    int lev1 = -1, lev2 = -1;

    int count = 1;//計算目前順位

    int next = node[first];

    while (next)

    {

        order1[next] = count++;

        next = node[next];

    }

    if (order1[second])//n this 祖先

        lev1 = order1[second];

    else

    {

        next = node[second];

        count = 1;

        while (next)

        {

            if (order1[next])//n this cousin

            {

                lev1 = order1[next];

                lev2 = count;

                break;

            }

            else if (next == first)//this n 的祖先

            {

                lev2 = count;

                break;

            }

            order2[next] = count++;

            next = node[next];

        }

    }

    return make_pair(lev1, lev2);

}

int main()

{

    string child, parent;

    map<string, int> Map;

    int mapId = 1;//0 是沒祖先

    while (cin >> child >> parent)

    {

        if (!strcmp(child.c_str(), "no.child"))

            break;

        if (!Map.count(child))

            Map[child] = mapId++;

        if (!Map.count(parent))

            Map[parent] = mapId++;

        

        node[Map[child]] = Map[parent];

    }

    cout<<node[1]<<" "<<node[2]<<""<<node[3]<<endl;


    string a, b;

    pair<int, int> relation;

    while (cin >> a >> b)

    {

        if (Map[a] == Map[b])//自己和自己沒關係

            puts("no relation");

        else

        {

            relation = getRelation(Map[a], Map[b]);

            int par1 = relation.first, par2 = relation.second;

            if (par1 != -1 && par2 != -1)

            {

                if (par1 == 1 && par2 == 1)//兄弟

                    puts("sibling");

                else//cousin

                {

                    int diff = abs(par1 - par2);

                    printf("%d cousin", min(par1, par2) - 1);

                    if (diff)

                        printf(" removed %d", diff);

                    putchar('\n');

                }

            }

            else if (par1 != -1)//a b 的子孫

            {

                if (par1 > 1)

                {

                    for (int i = 1; i < par1 - 1; i++)

                        printf("great ");

                    puts("grand child");

                }

                else

                    puts("child");

            }

            else if (par2 != -1)//a b 的祖先

            {

                if (par2 > 1)

                {

                    for (int i = 1; i < par2 - 1; i++)

                        printf("great ");

                    puts("grand parent");

                }

                else

                    puts("parent");

            }

            else//沒關係

                puts("no relation");

        }

    }

    return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值