原题链接https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805061769609216?type=7&page=1
AC代码
#include <bits/stdc++.h>
#include <string>
#define ll long long
#define PII std::pair<std::string,ll>
const ll N = 1e6;
ll n,m;
std::map<std::string,std::string> fu,mu;
std::map<std::string,std::string> op;
void add(std::string aa,std::string bb,std::string cc){
fu[aa] = bb;
mu[aa] = cc;
}
ll check(std::string aa,std::string bb){
std::map<std::string,ll> dj;
// ll res = std::stoll(aa);
std::queue<PII> pj1,pj2;
// ll ser = std::stoll(bb);
pj1.push({aa,1});
pj2.push({bb,1});
ll ok = 1;
while(pj1.size()){
auto jj = pj1.front();
pj1.pop();
std::string x = jj.first;
ll y = jj.second;
std::string r1 = fu[x];
std::string r2 = mu[x];
if(r1 != "" && r2 != "" && r1 != "-1" && r2 != "-1"){
if(op[r1] == op[r2]) return 2;
}
dj[r1] ++;
dj[r2] ++;
if(y >= 4) continue;
// std::cout << "这时候的数字是:" << r1 << "\n";
if(r1 != "")
pj1.push({r1,y + 1});
if(r2 != "")
pj1.push({r2,y + 1});
}
while(pj2.size()){
auto jj = pj2.front();
pj2.pop();
std::string x = jj.first;
ll y = jj.second;
std::string r1 = fu[x];
std::string r2 = mu[x];
dj[r1] ++;
dj[r2] ++;
if(r1 != "" && r2 != "" && r1 != "-1" && r2 != "-1"){
if(op[r1] == op[r2]) return 2;
}
if((dj[r1] == 2 && r1 != "-1" && r1 != "") || (dj[r2] == 2 && r2 != "-1" && r2 != "")){
ok = 3;
// break;
}
if(y >= 4) continue;
// std::cout << "这时候的数字是:" << r1 << "\n";
if(r1 != "")
pj2.push({r1,y + 1});
if(r2 != "")
pj2.push({r2,y + 1});
}
// std::cout << "最后到这里面来了:" << ok <<
return ok;
}
int main(){
std::cin >> n;
for(ll i = 1; i <= n; i ++){
std::string a,b,c,sex;
std::cin >> a >> sex >> b >> c;
add(a,b,c);
op[a] = sex;
op[b] = "M";
op[c] = "F";
}
std::cin >> m;
for(ll i = 1; i <= m; i ++){
std::string aa,bb;
std::cin >> aa >> bb;
if(op[aa] == op[bb] || aa == bb){//性别一样
std::cout << "Never Mind";
}
else{
if(check(aa,bb) == 1){//满足上面没有相同的人
std::cout << "Yes";
}
else if(check(aa,bb) == 2){//不能够满足所有可以的条件,就是根本不存在aa,bb 这两个人
std::cout << "Never Mind";
}
else std::cout << "No";//有相同的人
}
if(i != m) std::cout << "\n";
}
return 0;
}
/*
1
2 2
3 3 3 3
4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
*/