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
// 注意边输入数据边构建堆!!! STL库里有现成的make_heap,也可以自己写 // 接下来就是4个判断 // distance(H.begin(), find(H.begin(), H.end(), x)); 这句话是获取一个值的坐标,有2个函数 distance,find,具体用法请自行百度。 #include <iostream> #include <algorithm> #include <vector> #include <cmath> #include <string> using namespace std; void AdjustHeap(vector<int>& H,int root_index, int heap_length) { int left_child_index = root_index * 2 + 1; int right_child_index = root_index * 2 + 2; int min_index = root_index; if (right_child_index <= heap_length - 1) { if (H[left_child_index] < H[root_index]) { if (H[left_child_index] < H[right_child_index]) min_index = left_child_index; else min_index = right_child_index; } else { if (H[right_child_index] < H[root_index]) min_index = right_child_index; else min_index = root_index; } } if (left_child_index <= heap_length - 1 && right_child_index > heap_length - 1) { if (H[left_child_index] < H[root_index]) min_index = left_child_index; else min_index = root_index; } if (min_index != root_index) { swap(H[root_index], H[min_index]); AdjustHeap(H,min_index, heap_length); } return; } void make_heap(vector<int>& H, int heap_length) { for (int i = (heap_length + 1) / 2; i >= 0; i--) AdjustHeap(H, i, heap_length); } class greater_class { public: bool operator()(int a, int b) { return a > b; } }; int main() { //freopen("data.txt", "r", stdin); int N, M; int x, y; int temp; string str; cin >> N >> M; vector<int> H; for (int i = 0; i < N; i++) { cin >> temp; H.push_back(temp); //make_heap(H,H.size()); make_heap(H.begin(), H.end(), greater_class()); } getchar(); for (int i = 0; i < M; i++) { getline(cin, str); if (str.find("root", 0) != string::npos && sscanf(str.c_str(), "%d is the root", &x) == 1) { if (H[0] == x) cout << "T" << endl; else cout << "F" << endl; } if (str.find("siblings", 0) != string::npos && sscanf(str.c_str(), "%d and %d are siblings", &x, &y) == 2) { int index_x = distance(H.begin(), find(H.begin(), H.end(), x)); int index_y = distance(H.begin(), find(H.begin(), H.end(), y)); index_x++; index_y++; if (index_x / 2 == index_y / 2) cout << "T" << endl; else cout << "F" << endl; } if (str.find("parent", 0) != string::npos && sscanf(str.c_str(), "%d is the parent of %d", &x, &y) == 2) { int index_x = distance(H.begin(), find(H.begin(), H.end(), x)); int index_y = distance(H.begin(), find(H.begin(), H.end(), y)); if (index_y == index_x * 2 + 1 || index_y == index_x * 2 + 2) cout << "T" << endl; else cout << "F" << endl; } if (str.find("child", 0) != string::npos && sscanf(str.c_str(), "%d is a child of %d", &x, &y) == 2) { int index_x = distance(H.begin(), find(H.begin(), H.end(), x)); int index_y = distance(H.begin(), find(H.begin(), H.end(), y)); if (index_x == index_y * 2 + 1 || index_x == index_y * 2 + 2) cout << "T" << endl; else cout << "F" << endl; } } return 0; }