堆
题目链接
将一系列给定数字顺序插入一个初始为空的小顶堆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
注意原题要求一个一个的将数字插入堆中
C++代码
#include
#define x first
#define y second
#define send string::nops
using namespace std;
typedef long long ll;
const int N = 1e3 + 10;
const int M = 3 * N;
const int INF = 0x3f3f3f3f;
typedef pair PII;
int heap[N];
int n,m;
int main(){
string query;
cin>>n>>m;
int num = 0;
for(int i = 1;i <= n;i ++){
cin>>heap[i];
int t = i;
while(t / 2 > 0){
if(heap[t] < heap[t / 2])
swap(heap[t],heap[t / 2]);
else break;
t /= 2;
}
}
// cin.get();
int x,y;
scanf(" ");//清空缓存区
for(int i = 0;i < m;i ++){
cin>>x>>query;
if(query == "and"){
cin>>y;
int pox1 = FindIndex(x),pox2 = FindIndex(y);
if(pox1 / 2 == pox2 / 2 && pox1 != -1 && pox2 != -1)cout<<"T"<
else cout<<"F"<
}
else{
cin>>query;
if(query == "a"){
cin>>query>>query>>y;
int pox1 = FindIndex(x),pox2 = FindIndex(y);
if(pox2 == pox1 / 2 && pox1 != -1 && pox2 != -1)cout<<"T"<
else cout<<"F"<
}
else{
cin>>query;
if(query == "root"){
int pox = FindIndex(x);
if(pox == 1)cout<<"T"<
else cout<<"F"<
}
else{
cin>>query>>y;
int pox1 = FindIndex(x),pox2 = FindIndex(y);
if(pox2 / 2 == pox1 && pox1 != -1 && pox2 != -1)cout<<"T"<
else cout<<"F"<
}
}
}
getline(cin,query);
}
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/133737.html