1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAX_N = 1000 + 5; int m, n; int a[MAX_N]; bool maxHeap, minHeap; inline int ls(int o) { return o << 1; } inline int rs(int o) { return o << 1 | 1; } void judgeMaxHeap(int o) { if (ls(o) <= n && a[ls(o)] > a[o]) return (maxHeap = false), void(); if (rs(o) <= n && a[rs(o)] > a[o]) return (maxHeap = false), void(); if (ls(o) <= n) judgeMaxHeap(ls(o)); if (rs(o) <= n) judgeMaxHeap(rs(o)); } void judgeMinHeap(int o) { if (ls(o) <= n && a[ls(o)] < a[o]) return (minHeap = false), void(); if (rs(o) <= n && a[rs(o)] < a[o]) return (minHeap = false), void(); if (ls(o) <= n) judgeMinHeap(ls(o)); if (rs(o) <= n) judgeMinHeap(rs(o)); } void dfs(int o, vector <int> &tar) { if (ls(o) <= n) dfs(ls(o), tar); if (rs(o) <= n) dfs(rs(o), tar); tar.push_back(a[o]); } int main() { scanf("%d%d", &m, &n); for (int cs = 1; cs <= m; ++cs) { for (int i = 1; i <= n; ++i) { scanf("%d", a + i); } maxHeap = minHeap = true; judgeMaxHeap(1); judgeMinHeap(1); if (maxHeap) puts("Max Heap"); else if (minHeap) puts("Min Heap"); else puts("Not Heap"); vector <int> v; dfs(1, v); for (auto i = 0; i < v.size(); ++i) { printf("%d%c", v[i], i == v.size() - 1 ? '\n' : ' '); } } return 0; }
|