经典的模拟 经典的折磨
有一个细节看了别人才知道的
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
int n,q,m;
using pii = pair<int,string>;
map<string,pii>fa;
bool check(string a,string b){
if(!fa.count(a)||!fa.count(b))return true;
string anc="";
vector<string>anca,ancb;
set<string>visa,visb;
string ta = a,tb = b;
while(ta!=""){
anca.push_back(ta);
visa.insert(ta);
ta = fa[ta].second;
}
while(tb!=""){
ancb.push_back(tb);
visb.insert(tb);
tb = fa[tb].second;
}
int lena = anca.size();
int lenb = ancb.size();
for(int i=0;i<4&&i<lena;++i){
if(visb.count(anca[i]))return false;
}
for(int i=0;i<4&&i<lenb;++i){
if(visa.count(ancb[i]))return false;
}
return true;
}
void solve()
{
cin>>n;
while(n--){
string fname,sname;
cin>>fname>>sname;
if(sname.back()=='n')fa[fname] = {0,sname.substr(0,sname.size()-4)};
else if(sname.back()=='r')fa[fname] = {1,sname.substr(0,sname.size()-7)};
else if(sname.back()=='m')fa[fname] = {0,""};
else fa[fname] = {1,""};
}
cin>>q;
while(q--){
string a,b,c,d;cin>>a>>b>>c>>d;
if(!fa.count(a)||!fa.count(c)){cout<<"NA"<<"\n";continue;}
if(fa[a].first == fa[c].first){cout<<"Whatever"<<"\n";continue;}
if(check(a,c))cout<<"Yes"<<"\n";
else cout<<"No\n";
}
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _;
//cin>>_;
_ = 1;
while(_--)solve();
return 0;
}