开始用了一个很傻的办法,在建树的时候把所有正确的statement映射到1,最后判断的时候看读入的statement映射的值,是1就Yes, 0就No....可惜这样做最后一个测试点过不了,我估计是测试数据在前面加了0之类的,比如把"15 is the root"改成"015 is the root",这样我这个取巧的办法就通过不了……后来还是看柳婼的思路改了代码才AC的。
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
const int MAXN = 1001;
struct node{
int left;
int right;
int parent = -1;
int level;
};
int N, M, a, b, p;
std::vector<int> post, in;
std::vector<node> vec(MAXN);
std::map<int, int> mp;
std::string str;
bool flag = true;
int buildTree(int postL, int postR, int inL, int inR, int l){
if(postL == postR){
return -1;
}
int k = mp[post[postR - 1]];
int root = post[postR - 1];
vec[root].level = l;
vec[root].left = buildTree(postL, postL + k - inL, inL, k, l + 1);
vec[root].right = buildTree(postL + k - inL, postR - 1, k + 1, inR, l + 1);
if(vec[root].left * vec[root].right < 0){
flag = false;
}
if(vec[root].left > 0){
vec[vec[root].left].parent = root;
}
if(vec[root].right > 0){
vec[vec[root].right].parent = root;
}
return root;
}
int main(){
std::cin >> N;
post.resize(N);
in.resize(N);
for(int i = 0; i < N; ++i){
std::cin >> post[i];
}
for(int i = 0; i < N; ++i){
std::cin >> in[i];
mp[in[i]] = i;
}
int root = buildTree(0, N, 0, N, 0);
std::cin >> M;
for(int i = 0; i < M; ++i){
std::cin >> str;
if(str == "It"){
std::cout << (flag ? "Yes" : "No") << std::endl;
std::cin >> str >> str >> str >> str;
continue;
}
a = std::stoi(str);
std::cin >> str >> str;
if(str == "the"){
std::cin >> str;
if(str == "root"){
p = a == root ? 1 : 0;
} else if(str == "parent"){
std::cin >> str >> b;
p = vec[b].parent == a ? 1 : 0;
} else if(str == "left"){
std::cin >> str >> str >> b;
p = vec[b].left == a ? 1 : 0;
} else{
std::cin >> str >> str >> b;
p = vec[b].right == a ? 1 : 0;
}
} else{
b = std::stoi(str);
std::cin >> str >> str;
if(str == "siblings"){
p = vec[a].parent == vec[b].parent ? 1 : 0;
} else{
p = vec[a].level == vec[b].level ? 1 : 0;
std::cin >> str >> str >> str;
}
}
std::cout << (p == 1 ? "Yes" : "No") << std::endl;
}
return 0;
}
题目如下:
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.
Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:
- A is the root
- A and B are siblings
- A is the parent of B
- A is the left child of B
- A is the right child of B
- A and B are on the same level
- It is a full tree
Note:
- Two nodes are on the same level, means that they have the same depth.
- A full binary tree is a tree in which every node other than the leaves has two children.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are no more than 103 and are separated by a space.
Then another positive integer M (≤30) is given, followed by M lines of statements. It is guaranteed that both A
and B
in the statements are in the tree.
Output Specification:
For each statement, print in a line Yes
if it is correct, or No
if not.
Sample Input:
9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree
Sample Output:
Yes
No
Yes
No
Yes
Yes
Yes