L2-012 关于堆的判断 (25 分)
思路
小根堆的建立+字符串处理,真的好爱考字符串呀。。。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+9;
int a[N];
unordered_map<int,int> pos;
//堆化
void build(int n){
int t=n;
while(t/2>=1 && a[t/2]>a[t]){
swap(a[t/2],a[t]);
t/=2;
}
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++){
scanf("%d",a+i);
//在堆中插入
build(i);
}
for(int i=1; i<=n; i++) pos[a[i]]=i;
while(m--){
string s;
int x=0,y=0;
scanf("%d",&x);
while(cin>>s)
{
if(s=="and") {
scanf("%d",&y);
}
if(s=="root"){
if(pos[x]==1) puts("T");
else puts("F");
break;
}
else if(s=="siblings"){
if(pos[x]/2==pos[y]/2) puts("T");
else puts("F");
break;
}
else if(s=="parent"){
cin>>s;
scanf("%d",&y);
if(pos[x]==pos[y]/2) puts("T");
else puts("F");
break;
}
else if(s=="child"){
cin>>s;
scanf("%d",&y);
if(pos[x]/2==pos[y]) puts("T");
else puts("F");
break;
}
}
}
return 0;
}