L2-012. 关于堆的判断
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
将一系列给定数字顺序插入一个初始为空的小顶堆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<iostream>
#include<string>
using namespace std;
struct Judge
{
int num;
string judge;
};
void Insert(int h[],int _num,int n)
{
h[n] = _num;
int i = n;//用来判断是否有父节点
int j = (n - 1) / 2;
while ((i != 0) && (j >= 0))
{
if (h[j]<_num)
break;
h[i] = h[j];
i = j;
j = (i - 1) / 2;
}
h[i] = _num;
}
int getlocate(int h[], int num)//获取num的下标
{
int i = 0;
while (h[i] != num)
i++;
return i;
}
int main()
{
int count1;
int count2;
int num1;
int num2=0;
cin >> count1;
cin >> count2;
bool minus = false;
int g=1;//用于后面字符转数字
int loc;//用于后面定位
int bit;//数字位数
int len;
Judge *Jud = new Judge[count2];
int *H = new int[count1];
for (int i = 0;i < count1;i++)
{
cin >> num1;
Insert(H, num1, i);
}
for (int i = 0;i < count2;i++)
{
cin >> Jud[i].num;
getchar();//去掉数字和后面字符串之间的空格
getline(cin, Jud[i].judge);
}
for (int i = 0;i < count2;i++)
{
if (Jud[i].judge[0] == 'i')
{
if (Jud[i].judge[3] == 'a')//判断是否是兄弟
{
len = Jud[i].judge.length();
bit = len - 14;//计算数字的位数(包括负号)
g = 1;
num2 = 0;
while (bit-1)
{
g *= 10;
bit--;
}
int t = 0;//用来后面循环起始控制
if (Jud[i].judge[14] == '-')//判断是否负数
{
g /= 10;
t = 1;
minus = true;
}
for (int q = 14+t;q < len;q++)//计算num值(此时还是正数)
{
num2 = num2+int(Jud[i].judge[q]-'0')*g;
g /= 10;
}
if (minus == true)
num2 = -num2;
minus = false;
loc = getlocate(H, Jud[i].num);
if (loc == 0)//如果是根节点直接返回错误
cout << "F" << endl;
else if (H[(loc - 1) / 2] == num2)
cout << "T" << endl;
else
cout << "F" << endl;
}
else if (Jud[i].judge[7] == 'p')//判断是否是父节点
{
len = Jud[i].judge.length();
bit = len - 17;
g = 1;
num2 = 0;
while (bit-1)
{
g *= 10;
bit--;
}
int t = 0;
if (Jud[i].judge[17] == '-')
{
g /= 10;
t = 1;
minus = true;
}
for (int q = 17+t;q < len;q++)
{
num2 = num2 + int(Jud[i].judge[q]-'0')*g;
g /= 10;
}
if (minus == true)
num2 = -num2;
minus = false;
loc = getlocate(H,num2);
if (loc == 0)
cout << "F" << endl;
else if (H[(loc - 1) / 2] == Jud[i].num)
cout << "T" << endl;
else
cout << "F" << endl;
}
else if (Jud[i].judge[7] == 'r')
{
loc = getlocate(H, Jud[i].num);
if (loc == 0)
cout << "T" << endl;
else
cout << "F" << endl;
}
}
else
{
bit = 1;
while (Jud[i].judge[bit + 3] != ' ')
bit++;
bit--;
num2 = 0;
int t = bit;
g = 1;
while (t > 1)
{
g *= 10;
t--;
}
int v = 0;
if (Jud[i].judge[4] == '-')
{
g /= 10;
v = 1;
minus = true;
}
for (int q = 4+v;q < (4 + bit);q++)
{
num2 = num2 + int(Jud[i].judge[q]-'0')*g;
g /= 10;
}
if (minus == true)
num2 = -num2;
minus = false;
loc = getlocate(H, num2);
if (loc == 0)
cout << "F" << endl;
else if (loc % 2 == 0)
{
if (H[loc - 1]== Jud[i].num)
cout << "T" << endl;
else
cout << "F" << endl;
}
else
{
if (H[loc + 1] == Jud[i].num)
cout << "T" << endl;
else
cout << "F" << endl;
}
}
}
return 0;
}