题目描述
将一系列给定数字顺序插入一个初始为空的小顶堆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
分析:
根据题意本题需要我们建立一个小根堆,需要用到up()进行建堆,同时为了后序对两个结点间的关系进行判断,我们需要将建好的堆的值与其下标形成一种映射关系,所以需要用到map
在比较阶段我们只需用到一个string s用来抵消字符串和判断是哪一种情况,之后根据之前建好的map,对映射间的关系进行判断即可。
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int h[N];
int n,m;
map<int,int> mp;
void heap_swap(int a,int b)
{
swap(h[a],h[b]);
}
void up(int x)
{
while(x/2 && h[x/2]>h[x])
{
heap_swap(x/2,x);
x/=2;
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>h[i];
up(i);
}
for(int i=1; i<=n; i++) mp[h[i]]=i;
string s;
int x,y;
while(m--)
{
cin>>x;
cin>>s;
if(s[0]=='a') //x and y are siblings
{
cin>>y;
getline(cin,s);
if(mp[x]/2==mp[y]/2) puts("T");
else puts("F");
}
else
{
cin>>s;
cin>>s;
if(s[0]=='r') //x is the root
{
if(mp[x]==1) puts("T");
else puts("F");
}
else if(s[0]=='p') //x is the parent of y
{
cin>>s;
cin>>y;
if(mp[x]==mp[y]/2) puts("T");
else puts("F");
}
else //x is a child of y
{
cin>>s;
cin>>y;
if(mp[x]/2==mp[y]) puts("T");
else puts("F");
}
}
}
return 0;
}
该博客主要涉及数据结构中的小顶堆操作。通过C++实现将一系列数字插入空小顶堆,并利用映射关系判断节点间的父子、兄弟关系。输入包含节点数量和命题,输出命题的真假。关键操作包括建堆、节点位置映射和关系判断。
1914

被折叠的 条评论
为什么被折叠?



