比上一道堆稍微难了一点,但也只是一点,要求输出路径,写法类似算法笔记上面求最短路的时候,考虑所有可能的路径的时候的样子,然后注意一些细节就可以了,
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
int n;
int cnt1 = 0, cnt2 = 0, cnt = 0;
int heap[1005];
vi path;
void dfs(int root) {
if (root * 2 > n) {
path.pb(root);
for (int i = 0; i < (int) path.size(); i++) {
cout << heap[path[i]] << (i < (int) path.size() - 1 ? " " : "\n");
if (i && heap[path[i - 1]] >= heap[path[i]]) cnt1++;
if (i && heap[path[i - 1]] <= heap[path[i]]) cnt2++;
if (i) cnt++;
}
path.pop_back();
return;
}
path.pb(root);
if (root * 2 + 1 <= n) dfs(root * 2 + 1);
if (root * 2 <= n) dfs(root * 2);
path.pop_back();
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> heap[i];
}
dfs(1);
if (cnt1 == cnt) cout << "Max Heap\n";
else if (cnt2 == cnt) cout << "Min Heap\n";
else cout << "Not Heap\n";
return 0;
}