华为校园编码达人秀(第二季) 查找家谱

在华为校园编码达人秀第二季活动中,发现查找树的深度计算函数shen()存在错误,导致程序陷入无限循环。这个问题的解决成为了本次比赛的关键。

查找家谱
描述:

堂亲(又作一代堂亲),是我叔叔的儿子;我的二代堂亲,是我父亲的一代堂亲的儿子;

我的一代隔一代堂亲,是我的一代堂亲的儿子;

兄弟之间是零代堂亲关系; 

一般的说,任何两个人,如果其中一人是另一人的g代祖先(g>=1),他们之间的关系就是g-直系。

如果没有直系关系,但两人有共同的祖先,最近的共同祖先与其中一人相距(m+1)代,与另一人

相距(m+1+ n代,就称这两个人为m代隔n堂亲(m>=0,n>=0)。我们把这样的关系记作m-n-堂亲。

写一个程序,实现:

1)增加指定两人的父子关系;

2)找出任意指定两人之间的关系:堂亲、直系或没有关系。 

说明:

1、本考题中一个人有且只有一个名字,且不存在重名

2、自己和自己既不是直系也不是堂亲关系

运行时间限制: 无限制
内存限制: 无限制
输入:

输出一个整数N,说明后面输入有N行字符串。

每个字符串包括3个子串,以空格分隔。

第一个子串是命令字,后面是2个人的姓名

命令包括:

AddRelationShip Don Bill   增加Don是Bill的父亲

GetGeneration Don Bill     获取Don和Bill两人间的直系关系,如果两人间存在直系关系时,输出两人直系代数,否则输出-1

GetCousin  Don Bill     获取两人间的堂亲关系。如果两人间存在堂亲关系时,输出M N(M代隔N)堂亲,M和N之间一个空格隔开;否则输出-1

输出:

GetGeneration 和 GetCousin命令需要输出

GetGeneration 如果两人间存在直系关系时,输出两人直系代数,否则输出-1。

GetCousin 如果两人间存在堂亲关系时,输出M N(M代隔N)堂亲;否则输出-1。

样例输入:
7
AddRelationShip Don Bill
AddRelationShip Don Fred
AddRelationShip Bill John
AddRelationShip Fred Jake
GetGeneration Don Bill
GetGeneration Eric Sun
GetCousin John Jake
样例输出:
1
-1
1 0
答案提示:

 



查找树深度函数shen()有错!无法跳出循环。




#include<iostream>
#include<map>
using namespace std;
map <char*,int> hash1;
int par[101];
int rank1[100];

void init(int n)
{
    for(int i=0;i<n;i++)
    {
        par[i]=i;
        rank1[i]=0;
    }
}

int find1(int x)
{
    if(par[x]==x)
    {
        return x;
    }
    else
    {
        return par[x]=find1(par[x]);
    }
}

bool find2(int x,int y)
{
    if(rank1[x]>rank1[y])
    {
        while(rank1[x]>rank1[y])
        {
            if(par[y]==x) return true;
            else y=par[y];
        }
    }else if(rank1[x]<rank1[y])
    {
        while(rank1[x]<rank1[y])
        {
            if(par[x]==y) return true;
            else x=par[x];
        }
    }
    return false;
}
int shen(int x)
{
    int i=0;
    int dx=find1(x);
    while(x!=dx);
    {
        x=par[x];
        i++;
    }
    return i;

}
int ge(int x,int y)
{
    int i=0;
    x=par[x];
    while(1)
    {
        x=par[x];
        i++;
        int dy=y;
        while(par[dy]!=find1(dy))
        {
            if(par[dy]==x)
                return i;
            dy=par[dy];
        }
    }

}

void unite(int x,int y)
{
    x=find1(x);
    y=find1(y);
    if(x==y) return;

    if(rank1[x]<rank1[y])
    {
        par[x]=y;
    }
    else
    {
        par[y]=x;
        if(rank1[x]==rank1[y]) rank1[x]++;
    }
}

bool same(int x,int y)
{
    return find1(x)==find1(y);
}

int main(void)
{
    int N;
    int R=0;
    int id1,id2;
    init(100);
    cin>>N;
    for(int i=0;i<N;i++)
    {
        char m[20],m1[10],m2[10];
        cin>>m>>m1>>m2;
        if(m[3]=='R')
        {
            map<char*,int>::iterator ite;
            ite=hash1.find(m1);
            if(ite==hash1.end())
            {
                hash1[m1]=R;
                id1=R;
                R++;
            }else{
                id1=ite->second;
            }
            map<char*,int>::iterator ite1;
            ite1=hash1.find(m2);
            if(ite1==hash1.end())
            {
                hash1[m2]=R;
                id2=R;
                R++;
            }else{
                id2=ite1->second;
            }
            unite(id2,id1);
        }
        if(m[3]=='G')
        {
            map<char*,int>::iterator ite;
            ite=hash1.find(m1);
            id1=ite->second;
            map<char*,int>::iterator ite1;
            ite1=hash1.find(m2);
            id2=ite1->second;

            if(same(id1,id2)&&find2(id1,id2))
            {
                int daishu=shen(id1)-shen(id2);
                cout<<daishu<<endl;
            }

            else{
                cout<<"-1"<<endl;
            }
        }
        if(m[3]=='C')
        {
            map<char*,int>::iterator ite;
            ite=hash1.find(m1);
            id1=ite->second;
            map<char*,int>::iterator ite1;
            ite1=hash1.find(m2);
            id2=ite1->second;

            if(same(id1,id2)&&!find2(id1,id2))
            {
                int tshu=shen(id1)-shen(id2);
                int haha=ge(id1,id2);
                cout<<haha<<" "<<tshu<<endl;
            }

            else{
                cout<<"-1"<<endl;
            }
        }
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值