USACO 2018 February Contest, Bronze 3. Family Tree 模拟

http://www.usaco.org/index.php?page=viewproblem2&cpid=833&lang=zh

模拟就完事了

#include <bits/stdc++.h>
using namespace std;

const int maxn=105;
const int INF=1<<30;

int n;
string da[maxn];
string mo[maxn];

string find_mother(string cow){
    for (int i=0;i<n;i++){
        if (cow==da[i]){
            return mo[i];
        }
    }
    return "";
}

//如果cow1是cow2的直系祖先,返回cow1与cow2的代数
//否则返回-1 
int is_ancestor(string cow1,string cow2){
    int cnt=0;
    while (cow2!=""){
        if (cow1==cow2){
            return cnt;
        }
        cow2=find_mother(cow2);
        cnt++;
    }
    return -1;
}

int main(){

    string x,y;
    cin >> n;
    cin >> x >> y;
    for (int i=0;i<n;i++){
        cin >> mo[i] >> da[i];
    }

    string cow=x;
    int b=0;
    while (cow!=""){
        //如果cow是y的直系祖先
        if (is_ancestor(cow,y)!=-1){
            break;
        }
        cow=find_mother(cow);
        b++;
    }
    if (cow==""){
        cout << "NOT RELATED" << endl;
        return 0;
    }

    int a=is_ancestor(cow,y);

    if (a==1 && b==1) cout << "SIBLINGS" << endl;
    else
    if (a>1 && b>1) cout << "COUSINS" << endl;
    else{
        if (a>b) swap(x,y),swap(a,b);
        cout << y << " 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 " << x << endl;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值