1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include <bits/stdc++.h> using namespace std;
const int N = 100050;
struct person { string famn, firn, fan; int gender, fa; } p[N];
int n, m; map<string, int> mp;
int main() { cin >> n; for(int i=1; i<=n; i++) { cin >> p[i].firn >> p[i].famn; char lst = p[i].famn[p[i].famn.length()-1]; if(lst == 'm') { p[i].gender = 1; p[i].fan = p[i].famn.substr(0, p[i].famn.length()-1); } else if(lst == 'f') { p[i].gender = 0; p[i].fan = p[i].famn.substr(0, p[i].famn.length()-1); } else if(lst == 'n') { p[i].gender = 1; p[i].fan = p[i].famn.substr(0, p[i].famn.length()-4); } else if(lst == 'r') { p[i].gender = 0; p[i].fan = p[i].famn.substr(0, p[i].famn.length()-7); } mp[p[i].firn]=i; } for(int i=1; i<=n; i++) { char lst = p[i].famn[p[i].famn.length()-1]; if(lst !='m' && lst !='f' && mp[p[i].fan]) p[i].fa = mp[p[i].fan]; else p[i].fa = 0; }
cin >> m; for(int i=1; i<=m; i++) { string fmn1, fn1, fmn2, fn2; cin >> fn1 >> fmn1 >> fn2 >> fmn2; int id1=mp[fn1], id2=mp[fn2]; if(!id1 || !id2) cout << "NA" << endl; else if(p[id1].gender == p[id2].gender) cout << "Whatever" << endl; else { int fx, fy, i, j, flag = 0; for(fx=id1, i=1; fx ; fx=p[fx].fa, i++) { for(fy=id2, j=1; fy ; fy=p[fy].fa, j++) { if(i>=5 && j>=5) break; if(fx == fy && (i<5 || j<5)) flag=1; } } if(flag) cout << "No" << endl; else cout << "Yes" << endl; } } return 0; }
|