PTA - 家谱处理 ( 数据结构 )

这里写图片描述
这里写图片描述
这里写图片描述

思路

比赛的时候愣是用map< string, int>的值代表了这个人所处家谱中的层次
后来借鉴了小明学长的思路, 给每个人编id号, 用值表示每个对应的id
用fa[maxn]存的是每个人的父亲id, fa[id] = 父亲的id

有一个小问题, AC之后想关闭IO同步提一波速, 没料到PTA很玄学地直接给我返了WA???
本地测没问题, 检查代码也没有混用, 按理说PTA应该支持的呀? 唯一不确定的是getline(cin,s); 在关闭IO同步之后会产生什么特殊的化学反应吗?? 搜了一波资料也没找到相应的回答. 如果有清楚这个点是啥原因的dalao能够告诉我~ 靴靴~

AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <sstream>
#include <map>
#define mst(a) memset(a,0,sizeof(a));

using namespace std;

/*
parent      (直系)父
child       (直系)孩子
ancestor    祖先
sibling     同辈
descendant  后裔
*/
const int maxn = 100 + 5;
string s;
map<string, int> maps;
int a[maxn], fa[maxn];
vector<int> vec[maxn];

bool dfs(int a, int b){
    if( vec[a].size() == 0 )
        return false;
    for( size_t i = 0; i < vec[a].size(); i++ ){
        if( vec[a][i] == b ) return true;
        if( dfs(vec[a][i],b) ) return true;
    }
    return false;
}

bool solve( string stra, string strb, char ch ){
    int a = maps[stra], b = maps[strb];
    switch(ch){
case 'p' :  return a == fa[b]; break;
case 'c' :  return fa[a] == b; break;
case 'a' :  return dfs(a, b); break;
case 's' :  return fa[a] == fa[b]; break;
case 'd' :  return dfs(b, a); break;
    }
}

int main()
{
//    ios::sync_with_stdio(false);  //关IO同步就WA?
//    cin.tie(0);
    int n, test, cnt = 0;
    cin >> n >> test;
    getchar();
    while( n-- ){
        string name = "";
        s = "";
        getline(cin, s);
        int len = (int)s.size();
        int k = 0;
        for( int i = 0; i < len; i++ ){
            if( s[i] != ' ' ) name += s[i];
            else k++;
        }
        k /= 2;
        maps[name] = cnt++;
        int id = maps[name];
        if( k != 0 ){ //如果不是最大的祖先
            vec[a[k]].push_back(id);
            fa[id] = a[k];
        }
        else fa[id] = -1; //最大祖先没有father
        a[k+1] = id; //更新当前层数的father
    }

    while( test-- ){
        string n1, n2, n3, temp;
        cin >> n1 >> temp >> temp >> n2 >> temp >> n3;
        if( solve(n1,n3,n2[0]) ) cout << "True" << endl;
        else cout << "False" << endl;
    }
    return 0;
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值