堆的建立(L2-012 关于堆的判断)

include<bits/stdc++.h>

using namespace std;
const int N=10001;
int tre[N];

void up(int x)
{

if(x==1) return ;
else
{
if(tre[x/2]>tre[x]) swap(tre[x/2],tre[x]);
up(x/2);
}
return ;
}
int n,m;
int main() {
cin>>n>>m;

for(int i=1;i<=n;i++)
{
cin>>tre[i];
up(i);
}
cin.ignore();
while(m--)
{
string s;
getline(cin,s);
int a,b;
int x,y;
if(s.find("root")!=string::npos)
{
sscanf(s.c_str(),"%d is the root",&a);
if(tre[1]a) cout<<'T'<<endl;
else cout<<'F'<<endl;
}
else if(s.find("siblings")!=string::npos)
{
sscanf(s.c_str(),"%d and %d are siblings",&a,&b);
for(int i=1;i<=n;i++)
{
if(tre[i]
a) x=i;
if(tre[i]b) y=i;
}
// cout<<x<<" "<<y<<endl;
if(x/2
y/2) cout<<'T'<<endl;
else cout<<'F'<<endl;
}
else if(s.find("parent")!=string::npos)
{
sscanf(s.c_str(),"%d is the parent of %d",&a,&b);
for(int i=1;i<=n;i++)
{
if(tre[i]a) x=i;
if(tre[i]
b) y=i;
}
if(y/2x) cout<<'T'<<endl;
else cout<<'F'<<endl;
}
else{
sscanf(s.c_str(),"%d is a child of %d",&a,&b);
for(int i=1;i<=n;i++)
{
if(tre[i]
a) x=i;
if(tre[i]b) y=i;
}
if(x/2
y) cout<<'T'<<endl;
else cout<<'F'<<endl;
}
}
return 0;
}

posted on 2025-03-19 20:10  下头小美  阅读(32)  评论(0)    收藏  举报