输入样例:
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
就是字符串处理很繁琐别的还好。
找到自己的父亲的方式,先找到自己的位置,哪一个i的heap[i]=x,i<<1对应的值就是父亲的值
#include<cstdio>
using namespace std;
const int N=1000+5;
int heap[N<<2],n;
void push(int x)//create 堆
{
int tmp,flag=0;
while(x!=1&&!flag)
{
if(heap[x>>1]>heap[x])
tmp=heap[x],heap[x]=heap[x>>1],heap[x>>1]=tmp;
else
flag=1;
x>>=1;
}
}
int father(int x)//找到自己的父亲,通过枚举找到自己的位置,父亲的位置是自己的/2
{
int i;
for(i=1;i<=n;i++)
if(heap[i]==x)
break;
return heap[i>>1];
}
int main()
{
int m;scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&heap[i]);
push(i);
}
while(m--)
{
int x,y;char s[11];
scanf("%d%s",&x,s);
if(s[0]=='a') //siblings
{
scanf("%d%s",&y,s);scanf("%s",s);
printf(father(x)==father(y)?"T\n":"F\n");
}
else
{
scanf("%s",s);
if(s[0]=='a') //a child of who
{
scanf("%s",s);scanf("%s%d",s,&y);
printf(father(x)==y?"T\n":"F\n");
}
else
{
scanf("%s",s);
if(s[0]=='r') //the root;
printf(heap[1]==x?"T\n":"F\n");
else
{
scanf("%s%d",s,&y); //the parent of
printf(father(y)==x?"T\n":"F\n");
}
}
}
}
}