关于堆的判断 (25 分)
生活随笔
收集整理的這篇文章主要介紹了
关于堆的判断 (25 分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
將一系列給定數字順序插入一個初始為空的小頂堆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
分析:
STL中vector建立最大堆和最小堆相關知識
AC代碼:
#include<stdio.h> #include<string.h> #include<vector> #include<iostream> #include<algorithm> using namespace std; vector<int>ve; int Find(int x){return find(ve.begin()+1,ve.end(),x)-ve.begin(); } int main(){ve.push_back(0);string s;int a,n,m;cin>>n>>m;for(int i=0;i<n;i++){cin>>a;ve.push_back(a);push_heap(ve.begin()+1,ve.end(),greater<int>());}while(m--){int a,b;cin>>a>>s;if(s=="and"){cin>>b>>s>>s;a = Find(a);b = Find(b);if(a/2 == b/2) printf("T\n");//兄弟結點else printf("F\n");}else{cin>>s>>s;if(s=="root"){if(ve[1] == a) printf("T\n");//如果是根結點else printf("F\n");}else if(s=="parent"){cin>>s>>b;a = Find(a);b = Find(b);if(a == b/2) printf("T\n");//a是b的父親結點else printf("F\n");}else{cin>>s>>b;a = Find(a);b = Find(b);if(a/2 == b) printf("T\n");//a是b的兒子結點else printf("F\n");}}}return 0; }總結
以上是生活随笔為你收集整理的关于堆的判断 (25 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大腿环吸是什么意思
- 下一篇: 关于解决Path被大改,无法直接编辑恢复