技巧
先建立树,然后在建树的过程中
1.维护当前的深度
2.保存每个点的父节点
3.判断当前的点是否是全空或者全满,不是的话说明当前不是一个满树
AC
#include <bits/stdc++.h>
//#define int long long
#define eb emplace_back
#define pii pair<int,int>
#define endl "\n"
using namespace std;
using ll=long long;
const ll mod=1e9+7;
const int N=1e3+10;
int k,n,m;
struct node
{
int ls,rt,pa,level;
node()
{
ls=rt=pa=-1;
}
}nds[N];
int postorder[N],inorder[N];
int root,full=1;
int build(int h1,int h2,int l,int pa)
{
if(l<=0)return -1;
int cur=postorder[h1];//当前所在的节点的值
int i;
for(i=h2;;++i)if(postorder[h1]==inorder[i])break;
int l1=i-h2;
int l2=l-l1-1;
nds[cur].pa=pa;
nds[cur].level=nds[pa].level+1;
nds[cur].ls=build(h1-1-l2,h2,l1,cur);
nds[cur].rt=build(h1-1,i+1,l2,cur);
if(nds[cur].ls*nds[cur].rt<0)full=0;
return cur;
}
bool check()
{
string tmp;
string a,b,c,d;
cin>>a>>b>>c>>d;
// cout<<a<<" "<<b<<" "<<c<<" "<<d<<"==\n";
if(d=="are")
{
cin>>tmp;
if(tmp=="siblings")
{
int zhi1=stoi(a),zhi2=stoi(c);
if(nds[zhi1].pa==nds[zhi2].pa)return 1;
else return 0;
}
else
{
cin>>tmp;cin>>tmp;cin>>tmp;
int zhi1=stoi(a),zhi2=stoi(c);
if(nds[zhi1].level==nds[zhi2].level)return 1;
else return 0;
}
}
else if(d=="root")
{
int zhi1=stoi(a);
if(zhi1==root)return 1;
else return 0;
}
else if(d=="parent")
{
cin>>b;cin>>b;
int zhi1=stoi(a),zhi2=stoi(b);
if(nds[zhi2].pa==zhi1)return 1;
else return 0;
}
else if(d=="left")
{
cin>>b;cin>>b;cin>>b;
int zhi1=stoi(a),zhi2=stoi(b);
if(nds[zhi2].ls==zhi1)return 1;
else return 0;
}
else if(d=="right")
{
cin>>b;cin>>b;cin>>b;
int zhi1=stoi(a),zhi2=stoi(b);
if(nds[zhi2].rt==zhi1)return 1;
else return 0;
}
else if(d=="full")
{
cin>>tmp;
if(full)return 1;
else return 0;
}
}
void solve()
{
cin>>n;
for(int i=0;i<n;++i)cin>>postorder[i];
for(int i=0;i<n;++i)cin>>inorder[i];
root=build(n-1,0,n,1010);
// cout<<root->data;
cin>>m;
while(m--)
{
if(check())cout<<"Yes\n";
else cout<<"No\n";
}
}
int main()
//signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T=1;
while(T--)
{
solve();
}
return 0;
}
/*
*/