将一系列给定数字顺序插入一个初始为空的小顶堆 H[]。随后判断一系列相关命题是否为真。命题分下列几种:
- x is the root:x是根结点;
- x and y are siblings:x和y是兄弟结点;
- x is the parent of y:x是y的父结点;
- x is a child of y:x是y的一个子结点。
输入格式:
每组测试第1行包含2个正整数 N(≤ 1000)和 M(≤ 20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[−10000,10000]内的 N 个要被插入一个初始为空的小顶堆的整数。之后 M 行,每行给出一个命题。题目保证命题中的结点键值都是存在的。
输出格式:
对输入的每个命题,如果其为真,则在一行中输出 T ,否则输出 F 。
输入样例:
5 4
46 23 26 24 10
24 is the root
26 and 23 are siblings
46 is the parent of 23
23 is a child of 10
输出样例:
F
T
F
T
Code
#include<bits/stdc++.h>
#define ll long long
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const ll MAXN = 1e12 + 10;
ll n, m, i, j, k, t, w, x, y, z, in, flag, cou, sum, maxn, minn;
string s, s1, s2;
const ll INF = 1e9 + 7;
struct Relation
{
Relation *left, *right, *parents;
ll step, value;
};
map<ll, Relation>mm;
map<ll, ll>isexit;
void Buildtree ( ll *tree, ll x, ll len )
{
ll i;
for ( i = len; tree[i / 2] > x; i /= 2 ) tree[i] = tree[i / 2];
tree[i] = x;
}
Relation* ConfirmRelation ( ll *tree, ll flag, Relation *first, Relation *parents, ll step )
{
if ( first == NULL )
{
first = &mm[tree[flag]];
first->value = tree[flag];
first->parents = parents;
first->step = step;
first->left = first->right = NULL;
}
if ( flag * 2 <= n ) first->left = ConfirmRelation ( tree, flag * 2, first->left, first, step + 1 );
if ( flag * 2 + 1 <= n ) first->right = ConfirmRelation ( tree, flag * 2 + 1, first->right, first, step + 1 );
return first;
}
int main()
{
FAST_IO;
cin >> n >> m;
ll tree[n];
for ( tree[0] = -10001, i = 1; i <= n; i++ )
cin >> j, Buildtree ( tree, j, i ), isexit[j] = 1;
Relation *first =NULL;first= ConfirmRelation ( tree, 1, first, NULL, 1 );
while(m--){
flag=0;
cin>>x>>s;
if(s=="and"){
cin>>y>>s>>s;
flag=(isexit[x]&&isexit[y]&&mm[x].parents!=NULL&&mm[y].parents!=NULL&&mm[x].parents==mm[y].parents);
}else{
cin>>s;
if(s=="a"){
cin>>s>>s>>y;
flag=(isexit[x]&&isexit[y]&&mm[x].parents!=NULL&&mm[x].parents->value==y);
}else{
cin>>s;
if(s=="root")flag=(isexit[x]&&mm[x].parents==NULL);
else{
cin>>s>>y;
flag=(isexit[x]&&isexit[y]&&mm[y].parents!=NULL&&x==mm[y].parents->value);
}
}
}
cout<<(flag?"T":"F")<<endl;
}
return 0;
}