1167 Cartesian Tree (30 point(s))
题目
A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of everyone.
Now given a set of tentative arrangements, your job is to tell the organizers whether or not each area is all set.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 200), the number of heads in the summit, and M, the number of friendship relations. Then M lines follow, each gives a pair of indices of the heads who are friends to each other. The heads are indexed from 1 to N.
Then there is another positive integer K (≤ 100), and K lines of tentative arrangement of rest areas follow, each first gives a positive number L (≤ N), then followed by a sequence of L distinct indices of the heads. All the numbers in a line are separated by a space.
Output Specification:
For each of the K areas, print in a line your advice in the following format:
- if in this area everyone is a direct friend of everyone, and no friend is missing (that is, no one else is a direct friend of everyone in this area), print
Area X is OK.
. - if in this area everyone is a direct friend of everyone, yet there are some other heads who may also be invited without breaking the ideal arrangement, print
Area X may invite more people, such as H.
whereH
is the smallest index of the head who may be invited. - if in this area the arrangement is not an ideal one, then print
Area X needs help.
so the host can provide some special service to help the heads get to know each other.
Here X
is the index of an area, starting from 1 to K
.
Sample Input:
8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1
2 4 6
3 3 2 1
Sample Output:
Area 1 is OK.
Area 2 is OK.
Area 3 is OK.
Area 4 is OK.
Area 5 may invite more people, such as 3.
Area 6 needs help.
思路
题意
给出n(n<=30)个中序输出的最小堆的key,根据这些中序输出得到该最小堆的层序输出。
思路
树不大,所以所有数据结构都可以用数组暴力解决。用一维数组numbers[i]
存储中序的结点,用二维数组tree[i][2]
存储结点index为i的左右孩子(不建议直接用结点本身的值做为关键字,因为范围是int,需要建树,很麻烦)。函数FindMin()
找到当前序列的最小值,即root,然后从根开始用BuildTree()
函数递归建树。建好树后用queue层序输出每个树结点对应的数字即可。
解法
#include<bits/stdc++.h>
using namespace std;
int n;
int numbers[35];
int tree[35][2];//tree[i][0]-left, tree[i][1]-right
int FindMin(int start, int end) {
int min = start;
for (int i = start + 1; i <= end; i++) {
if (numbers[i] < numbers[min])
min = i;
}
return min;
}
void BuildTree(int ll, int root, int rr) {
if (ll < root) {
tree[root][0] = FindMin(ll, root - 1);
BuildTree(ll, tree[root][0], root - 1);
}
else tree[root][0] = -1;
if (root < rr) {
tree[root][1] = FindMin(root + 1, rr);
BuildTree(root + 1, tree[root][1], rr);
}
else tree[root][1] = -1;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}
int root = FindMin(0, n - 1);
BuildTree(0, root, n - 1);
queue<int> q;
q.push(root);
int cnt = 0;
while (!q.empty()) {
cnt++;
int node = q.front();
printf("%d%s", numbers[node], cnt != n ? " " : "\n");
q.pop();
if (tree[node][0] != -1) q.push(tree[node][0]);
if (tree[node][1] != -1) q.push(tree[node][1]);
}
return 0;
}