Topic description
Inserts a sequence of given numbers sequentially into an initially empty small top heap H[]. Then it is determined whether a series of related propositions are true. The titles are divided into the following categories:
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的一个子结点。
Input format:
The first line of each test group contains two positive integers N (≤ 1000) and M (≤ 20), which are the number of inserted elements and the number of propositions to be judged. The next line gives the N integers in the interval [−10000, 10000] to be inserted into an initially empty small top heap. After M lines, each line gives a proposition. The title guarantees that the node keys in the proposition all exist.
Output format:
For each proposition of the input, if it is true, output T in one line, otherwise output F.
Input sample:
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
Sample output:
F
T
F
T
heap row
Process strings one by one, the dumbest way is also the most efficient
#include <iostream>
using namespace std;
const int N = 1010;
int n, m;
int h[N];
void up(int u) {
while (u / 2 && h[u/2] > h[u]) {
swap(h[u/2], h[u]);
u /= 2;
}
}
void down(int u) {
int t = u;
if (2*u <= n && h[2*u] < h[t]) t = 2*u;
if (2*u+1 <= n && h[2*u+1] < h[t]) t = 2*u+1;
if (t != u) {
swap(h[t], h[u]);
down(t);
}
}
int find(int x) {
for (int i = 1; i <= n; i++)
if (h[i] == x) return i;
return 0;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> h[i];
up(i);
}
while (m--) {
int x;
string s1;
cin >> x >> s1;
if (s1 == "is") {
string s2;
cin >> s2;
if (s2 == "the") {
string s3;
cin >> s3;
if (s3 == "root") {
if (find(x) == 1) cout << 'T' << endl;
else cout << 'F' << endl;
} else {
string s4;
int y;
cin >> s4 >> y;
if (find(x) == find(y)/2) cout << 'T' << endl;
else cout << 'F' << endl;
}
} else if (s2 == "a") {
string s3, s4;
int y;
cin >> s3 >> s4 >> y;
if (find(x)/2 == find(y)) cout << 'T' << endl;
else cout << 'F' << endl;
}
} else if (s1 == "and") {
int y;
string s2, s3;
cin >> y >> s2 >> s3;
if (find(x)/2 == find(y)/2) cout << 'T' << endl;
else cout << 'F' << endl;
}
}
return 0;
}