pat 1155 Heap Paths

pat 1155 Heap Paths


// 本题主要是 表示if(!) 只有 0 才能表示 if(-1) 同样会输出元素
#include<bits/stdc++.h>
using namespace std;
const int maxsize = 1005;
int n, isHeap = 1, maxHeap = 1;
vector<int> tree, path, father;
void getPath(int index, int f) {
    if(index >= n) return;
    if(index * 2 + 1 < n) {
        if(index * 2 + 2 < n) getPath(index * 2 + 2, index);
        getPath(index * 2 + 1, index);
    } else {
        path.push_back(index);
    }
    father[index] = f;
}
bool cmp(int a, int b) {
    return a > b;
}
int main()
{
    scanf("%d", &n);
    tree.resize(n), father.resize(n);
    for(int i = 0; i < n; i++) scanf("%d", &tree[i]);
    maxHeap = tree[0] >= tree[1] ? 1 : 0;
    getPath(0, -1);
    for(int i = 0; i < path.size(); i++) {
        //printf("%d", tree[path[i]]);
        vector<int> res, temp;
        int k = path[i];
        temp.push_back(tree[path[i]]);
        while(father[k] != -1) {
            //printf(" %d", tree[father[k]]);
            temp.push_back(tree[father[k]]);
            k = father[k];
        }
        printf("%d", temp[temp.size() - 1]);
        for(auto i = temp.rbegin() + 1; i != temp.rend(); i++) {
            printf(" %d", *i);
        }
        printf("\n");
        res = temp;
        if(maxHeap) {
            sort(res.begin(), res.end());
        } else
            sort(res.begin(), res.end(), cmp);
        for(int i = 0; i < res.size(); i++) {
            if(res[i] != temp[i]) {
                isHeap = 0;
            }
        }
    }
    if(isHeap) {
        if(maxHeap)
            printf("Max Heap\n");
        else
            printf("Min Heap\n");
    } else {
        printf("Not Heap\n");
    }
    return 0;
}

posted @ 2020-03-05 19:49  yxdh  阅读(126)  评论(0编辑  收藏  举报