[GPLT]L2-012关于堆的判断

L2-012. 关于堆的判断

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

将一系列给定数字顺序插入一个初始为空的小顶堆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
分析:必须注意,因为题目要求按照插入的顺序建立,
所以是边插入边调整的,必须用向上调整,每次输入一个数之后就将它向上调整。
(两者建立出来的二叉树不同)而不能采用先转换为二叉树的方式再向下调整。

建堆方式传送:https://blog.csdn.net/m0_38013346/article/details/79744749

 
 
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1010;
class Heap {
private:
    vector<int> heap;
public:
    Heap(){ heap.clear();heap.push_back(0);}
    void shiftup(int pos);
    void shiftdown(int pos);
    void push(int x);
    void pop();
    int top();
    int getlen() {return heap.size()-1;}
    int getnum(int pos) {return heap[pos];}
};
void Heap::shiftup(int pos) {
    while(pos > 1) {
        if(heap[pos] < heap[pos>>1]) {
            swap(heap[pos],heap[pos>>1]);
            pos >>= 1;
        }
        else return;
    }
}
void Heap::shiftdown(int pos) {
    while((pos<<1)<heap.size())
    {
        int son = pos << 1;
        if(son+1<heap.size() && heap[son+1] < heap[son]) son++; /// left or right son
        if(heap[son] < heap[pos]) {
            swap(heap[son],heap[pos]);
            pos = son;
        }
        else return;
    }
}
int Heap::top() {
    if(heap.size()>1) return heap[1];
    return -INF;
}
void Heap::pop() {
    swap(heap[1],*(heap.end()-1));
    heap.pop_back();
    shiftdown(1);
}
void Heap::push(int x) {
    heap.push_back(x);
    shiftup(heap.size()-1);
}

Heap heap;

void judge1(int x) {
    if(heap.top() == x) printf("T\n");
    else printf("F\n");
}
void judge2(int x,int y) {
    int i = 1;
    while((i<<1)+1 <= heap.getlen()) {
        int lson = i<<1,rson = (i<<1)+1;
        if((heap.getnum(lson) == x && heap.getnum(rson) == y)||
           (heap.getnum(lson) == y && heap.getnum(rson) == x)) {
            printf("T\n");
            return ;
        }
        i++;
    }
    printf("F\n");
}
void judge3(int x,int y) {
    int i = 1;
    bool flag = false;
    while( (i<<1) <= heap.getlen()) {
        if(heap.getnum(i)!=x) {i++;continue;}
        int lson = i << 1;
        if(heap.getnum(lson) == y) flag = true;
        if(lson + 1 <= heap.getlen() && heap.getnum(lson+1) == y) flag = true;
        if(flag) break;
        i++;
    }
    if(flag) printf("T\n");
    else printf("F\n");
}
void judge4(int x,int y) {
    judge3(y,x);
}
int main()
{
    int n,m,x,y;
    char str[10];
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) {
        scanf("%d",&x);
        heap.push(x);
    }
    for(int i=0;i<m;i++) {
        scanf("%d",&x);
        scanf("%s",str);
        if(str[0] == 'i') {
            scanf("%s",str);
            if(str[0] == 'a') {
                scanf("%s%s",str,str);
                scanf("%d",&y);
                judge4(x,y);
            }
            else {
                scanf("%s",str);
                if(str[0] == 'r') {
                    judge1(x);
                }
                else {
                    scanf("%s%d",str,&y);
                    judge3(x,y);
                }
            }
        }
        else {
            scanf("%d",&y);
            scanf("%s%s",str,str);
            judge2(x,y);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值