L2-012 关于堆的判断 (25 分)
将一系列给定数字顺序插入一个初始为空的小顶堆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
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1) (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
int a[N];
void fun(int pos){
while(pos>1){
if(a[pos]<a[pos/2]){
swap(a[pos],a[pos/2]);
pos/=2;
}
else break;
}
}
char s1[100];
int gun(){
int y=0;
for(int i=0;i<strlen(s1);i++){
if(s1[i]>='0'&&s1[i]<='9'){
int j=i;
while(s1[j]>='0'&&s1[j]<='9'){
y=y*10+(s1[j]-'0');
j++;
}
if(s1[i-1]=='-')
y*=-1;
break;
}
}
return y;
}
map<int,int>mapp;
int main(){
int n,m,x;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
fun(i);
}
for(int i=1;i<=n;i++) mapp[a[i]]=i;
while(m--){
scanf("%d",&x);
getchar();
scanf("%[^\n]",&s1);
if(strstr(s1,"root")){
if(a[1]==x) printf("T\n");
else printf("F\n");
}
else if(strstr(s1,"siblings")){
int y=gun();
int h1=mapp[x],h2=mapp[y];
if(h1/2==h2/2)
printf("T\n");
else
printf("F\n");
}
else if(strstr(s1,"parent")){
int y=gun();
if(a[mapp[y]/2]==x)
printf("T\n");
else
printf("F\n");
}
else{
int y=gun();
if(a[mapp[x]/2]==y)
printf("T\n");
else
printf("F\n");
}
}
return 0;
}