uva 115

来源:互联网 发布:黑门市场 知乎 编辑:程序博客网 时间:2024/04/30 06:26

#include<cstdio>

#include<iostream>

#include<string>

#include<cstring>

#include<map>

#include<utility>

#include<algorithm>

#define N 301

using namespacestd;

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;

            }

            elseif (next == first)//this n的祖先

            {

                lev2 = count;

                break;

            }

            order2[next] = count++;

            next = node[next];

        }

    }

    returnmake_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');

                }

            }

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

            {

                if (par1 >1)

                {

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

                        printf("great ");

                    puts("grand child");

                }

                else

                    puts("child");

            }

            elseif (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");

        }

    }

    return0;

}

0 0
原创粉丝点击