L2-012 关于堆的判断 (25 分)

    xiaoxiao2022-07-13  161

    #include<iostream> #include<vector> #include<set> #include<string> #include<algorithm> #include<string.h> #include<sstream> #define iIN(x,y) for(int i=x;i<y;i++) using namespace std; int heap[1005]; int N,M; void adjust(){ //建堆:从第零个开始调整 iIN(1,N+1){ int now=i; while(now!=1){ if(heap[now/2]>heap[now]){ //此结点比父节点小(小顶堆父节点应是最小的) int box=heap[now/2]; heap[now/2]=heap[now]; heap[now]=box; } else break; now/=2;//接着就调整父节点 } } } int getlocate(int num){ iIN(1,N+1){ if(heap[i]==num)return i; } } int sTOint(string s){ stringstream water; water<<s; int box; water>>box; return box; } string s[10]; int main(){ cin>>N>>M; iIN(1,N+1){ cin>>heap[i]; } adjust(); string st; getchar(); while(M--){ getline(cin,st); stringstream water(st); int i=0; while(water>>s[i++]); //以下处理s数组即可 if(s[3]=="root"){ //s[0] 是根节点? if(heap[1]==sTOint(s[0]))cout<<"T"<<endl; else cout<<"F"<<endl; } else if(s[3]=="are"){ //s[0] s[2]是兄弟结点 int n1=getlocate(sTOint(s[0])); int n2=getlocate(sTOint(s[2])); if(n1/2==n2/2)cout<<"T"<<endl; else cout<<"F"<<endl; } else if(s[3]=="parent"){ //s[0]是s[5]的父节点 if(getlocate(sTOint(s[5]))/2==getlocate(sTOint(s[0])))cout<<"T"<<endl; else cout<<"F"<<endl; } else{ if(getlocate(sTOint(s[0]))/2==getlocate(sTOint(s[5])))cout<<"T"<<endl; else cout<<"F"<<endl; } } return 0; }

     

    最新回复(0)