将一系列给定数字顺序插入一个初始为空的小顶堆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
注意:
是要按 顺序插入一个初始为空的小顶堆H[]
。
将值都加上10000,就可以将值都变为 大于等于 0的数。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1010;
int heap[N];
int id;
int p[20010];
void down(int x)
{
int t = x;
if(2 * x <= id && heap[2 * x] < heap[t]) t = 2 * x;
if(2 * x + 1 <= id && heap[2 * x + 1] < heap[t]) t = 2 * x + 1;
if(t != x)
{
swap(heap[t],heap[x]);
down(t);
}
}
void up(int x)
{
while(x > 1 && heap[x] < heap[x / 2])
{
swap(heap[x],heap[x / 2]);
x /= 2;
}
}
int convert(string& str,int t)
{
int flag = 0;
if(str[t] == '-') flag = 1,t++;
int res = 0;
for(int i = t;'0' <= str[i] && str[i] <= '9';i++)
res = res * 10 + str[i] - '0';
if(flag) res = 0 - res;
return res;
}
int main()
{
int n = 0,m = 0;
cin >> n >> m;
id = n;
for(int i = 1;i <= n;i++)
{
cin >> heap[i],heap[i] += 10000;
up(i);
}
//for(int i = n / 2;i;i--) down(i);
for(int i = 1;i <= n;i++) p[heap[i]] = i;
string str;
getline(cin,str);
for(int i = 0; i < m;i++)
{
getline(cin,str);
int flag = 0,x = 0,y = 0;
if(str.find("root") != -1)
{
x = convert(str,0);
x += 10000;
if(heap[1] == x) flag = true;
}
else if(str.find("siblings") != -1)
{
x = convert(str,0),y = convert(str,str.find("and") + 4);
x += 10000, y += 10000;
if(p[x] / 2 == p[y] / 2) flag = true;
}
else if(str.find("parent") != -1)
{
x = convert(str,0),y = convert(str,str.find("of") + 3);
x += 10000,y+=10000;
if(p[x] == p[y] / 2) flag = 1;
}
else
{
x = convert(str,0),y = convert(str,str.find("of") + 3);
x += 10000,y+=10000;
if(p[x] / 2 == p[y]) flag = 1;
}
// cout << x << " " << y << endl;
cout << (flag ? "T\n" : "F\n");
}
return 0;
}
二刷代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
using namespace std;
const int N = 1010;
int n,m;
int he[N];
unordered_map<int,int> p;
void up(int x)
{
while(x > 1 && he[x] < he[x / 2])
{
swap(he[x],he[x / 2]);
x /= 2;
}
}
int main()
{
cin >> n >> m;
for(int i = 1;i <= n;i++)
{
cin >> he[i];
up(i);
}
for(int i = 1;i <= n;i++) p[he[i]] = i;
string str;
getline(cin,str);
while(m--)
{
getline(cin,str);
int x = 0,y = 0,flag = 0;
if(str.back() == 't')
{
sscanf(str.c_str(),"%d is the root",&x);
if(p[x] == 1) flag = 1;
}
else if(str.back() == 's')
{
sscanf(str.c_str(),"%d and %d are siblings",&x,&y);
if(p[x] / 2 == p[y] / 2) flag = 1;
}
else if(str.find("parent") != -1)
{
sscanf(str.c_str(),"%d is the parent of %d",&x,&y);
if(p[x] == p[y] / 2) flag = 1;
}
else
{
sscanf(str.c_str(),"%d is a child of %d",&x,&y);
if(p[y] == p[x] / 2) flag = 1;
}
if(flag) puts("T");
else puts("F");
}
return 0;
}
结果: