Skip to content

L2-012 关于堆的判断

Statement

Metadata

  • 作者: 陈越
  • 单位: 浙江大学
  • 代码长度限制: 16 KB
  • 时间限制: 400 ms
  • 内存限制: 64 MB

将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:

  • x is the rootx是根结点;
  • x and y are siblingsxy是兄弟结点;
  • x is the parent of yxy的父结点;
  • x is a child of yxy的一个子结点。

输入格式

每组测试第1行包含2个正整数N\le 1000)和M\le 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

Solution

#include <ctype.h>
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define CLR(a) memset(a, 0, sizeof(a))

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;

const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;

int main() {
    map<int, int> vis;
    int n, m, num;
    scanf("%d%d", &n, &m);
    vector<int> v;
    for (int i = 0; i < n; i++) {
        scanf("%d", &num);
        num = -num;
        v.push_back(num);
        push_heap(v.begin(), v.end());
    }
    for (int i = 0; i < n; i++) vis[-v[i]] = i + 1;
    while (m--) {
        int a, b, c, d;
        string s;
        cin >> a;
        cin >> s;
        if (s == "and")  // 2
        {
            cin >> b;
            cin >> s;
            cin >> s;
            c = vis[a];
            d = vis[b];
            if (v[c / 2] == v[d / 2] && c != d)
                cout << "T\n";
            else
                cout << "F\n";
        } else {
            cin >> s;
            if (s == "the") {
                cin >> s;
                if (s == "root")  // 1
                {
                    if (vis[a] == 1)
                        cout << "T\n";
                    else
                        cout << "F\n";
                } else  // 3
                {
                    cin >> s;
                    cin >> b;
                    c = vis[a];
                    d = vis[b];
                    if (d / 2 == c)
                        cout << "T\n";
                    else
                        cout << "F\n";
                }
            } else  // 4
            {
                cin >> s;
                cin >> s;
                cin >> b;
                c = vis[a];
                d = vis[b];
                if (c / 2 == d)
                    cout << "T\n";
                else
                    cout << "F\n";
            }
        }
    }
}

Last update: May 4, 2022
Back to top