L2-030 冰岛人 (25 分) (最近公共祖先 思维

该代码实现了一个社交网络中用户性别关系的处理程序。它读取用户信息,包括性别和关系,并进行字符串匹配来判断两个用户是否相关。主要功能包括用户ID映射、性别判断和关系路径检查。程序在遇到未知用户时返回'NA',并根据用户性别差异输出相应结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加链接描述

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
unordered_map<string, int> sex;
unordered_map<string, int> id;
unordered_map<int, string> name;
struct node {
    string s;
};
unordered_map<string,node>arr;
int tot = 1, flag;
bool check(string a,string b)
{
    // cout << name[u] << "\n"; 
    int tota=0;
    while(a!=""){
        tota++;
        
        int totb=0;
        string p=b;
        while(p!=""){
            totb++;
            if(a==p&&(tota<=4||totb<=4)){
                return 0;
            }
            if(tota>4&&totb>4)return 1;
            p=arr[p].s;
        }
        a=arr[a].s;

    }
    return 1;
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        string a, b, A, B;
        cin >> a >> b;
        if (id[a] == 0)
        {
            id[a] = tot;
            name[tot++] = a;
        }

        if (b.back() == 'm')
        {

            B = b.substr(0, b.size() - 1), sex[a] = 1;
            
        }
        else if (b.back() == 'f')
            sex[a] = 2, B = b.substr(0, b.size() - 1);
        else if (b.back() == 'n')
        {
            sex[a] = 1;
            B = b.substr(0, b.size() - 4);
            if (id[B] == 0)
            {
                id[B] = tot;
                name[tot++] = B;
            }
            arr[a]={B};
            // cout<<a<<" "<<id[a]<<" "<<id[B]<<"\n";
            // v[id[a]].push_back(id[B]);
        }
        else if (b.back() == 'r')
        {
            sex[a] = 2;
            B = b.substr(0, b.size() - 7);
            if (id[B] == 0)
            {
                id[B] = tot;
                name[tot++] = B;
            }
            arr[a]={B};
            //   cout<<a<<" "<<id[a]<<" "<<id[B]<<"\n";
            // v[id[a]].push_back(id[B]);
        }
        // v[id[a]].push_back(id[B]);
    }
    int q;
    cin >> q;
    while (q--)
    {
        string a1, b1, a2, b2;
        cin >> a1 >> b1 >> a2 >> b2;
        if (id[a1] == 0 || id[a2] == 0)
        {
            cout << "NA\n";
            continue;
        }
        if (sex[a1] == sex[a2])
        {
            cout << "Whatever\n";
            continue;
        }
       
        
        flag = 0;
        flag=check(a1, a2);
        
        if (!flag)
        {
            cout << "No\n";
        }
        else
            cout << "Yes\n";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值