要判断给出的点是否出现过,不然即使在同一层或者是兄弟节点也是无效的。
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cctype>
#include <unordered_map>
#include <map>
using namespace std;
const int N = 35;
int post[N], in[N];
unordered_map<int, int> mp, pm;
typedef pair<int, string> PII;
int tag;
struct tree{
int val;
tree* left, *right;
tree(int x){
val = x;
left = right = nullptr;
}
};
tree* create(int pl,int pr, int inl, int inr, int ind){
if(pl > pr) return nullptr;
tree* root = new tree(post[pr]);
pm[post[pr]] = ind;
int k = mp[post[pr]];
int leftnum = k - inl;
root->left = create(pl, pl+leftnum-1, inl, k -1, 2*ind);
root->right = create(pl+leftnum, pr-1, k+1, inr, 2*ind+1);
return root;
}
void dfs(tree* root){
if(!root) return;
if(!root->left&&root->right) tag = 1;
if(root->left&&!root->right) tag = 1;
dfs(root->left);
dfs(root->right);
}
int main() {
int n;
cin>>n;
for(int i = 0; i < n; i++){
cin>>post[i];
}
for(int i = 0; i < n; i++){
cin>>in[i];
mp[in[i]] = i;
}
tree* root = create(0, n-1, 0, n-1, 1);
int m;
tag = 0;
dfs(root);
cin>>m;
string str, s;
while(m--){
cin>>str;
if(str == "It"){
cin>>str>>str>>str>>str;
if(!tag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}else{
int a = stoi(str);
cin>>str;
if(str == "and"){
int b;
cin>>b;
cin>>str>>str;
if(str == "on"){
cin>>str>>str>>str;
if(pm.count(a)&&pm.count(b)){
int u = pm[a], v = pm[b];
int lvu = log(u)/log(2);
int lvv = log(v)/log(2);
if(lvu == lvv) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}else cout<<"No"<<endl;
}else{
if(pm.count(a)&&pm.count(b)){
int u = pm[a], v = pm[b];
if(u/2 == v/2) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}else cout<<"No"<<endl;
}
}else{
cin>>str>>str;
if(str=="root"){
if(pm.count(a)){
int u = pm[a];
if(u==1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}else cout<<"No"<<endl;
}else if(str=="left"){
cin>>str>>str;
int b;
cin>>b;
if(pm.count(a)&&pm.count(b)){
int u = pm[a], v = pm[b];
if(u == 2*v) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}else cout<<"No"<<endl;
}else if(str == "right"){
cin>>str>>str;
int b;
cin>>b;
if(pm.count(a)&&pm.count(b)){
int u = pm[a], v = pm[b];
if(u == 2*v+1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}else cout<<"No"<<endl;
}else{
cin>>str;
int b;
cin>>b;
if(pm.count(a)&&pm.count(b)){
int u = pm[a], v = pm[b];
if(u == v/2) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}else cout<<"No"<<endl;
}
}
}
}
return 0;
}