原题目参见:1143 Lowest Common Ancestor (30分)
思路:查找第一个数的过程中标记所有访问过的节点。在查找第二个数的时候,所有访问过的路径节点,如果被第一个数字访问过,则说明这个节点是两个数字的共同祖先,将答案更新为此节点,直至最深处的一个共同节点,该答案即为所求。
因为第一个数据访问的路径上标志位已经被修改,所以需要记录第一次路径中的最后一个节点,通过递推访问其父节点,重置路径上的标志位,即可保证在每次查询时候所有的标志位都是干净的。
另外注意,输入的树由于不满足红黑树的性质,仅仅是普通的查找二叉树,所以很可能存在如1 2 3 4 5 6 … 10,000,退化成一条链式结构,使用自己写的二叉查找树,每次查询可能会从O(logn)(约14)退化为O(n)(最差10000)的查询时间。
不过经测试,即使采用自己写的二叉查找树,测试点4的时间用时约190ms,不会超过题目要求的200ms。如果采用自己书写的二叉查找树,可以设置一个剪枝条件:在处理输入时,保存树中的最小和最大节点,如果查询值超过了树的表示范围,则可以直接判定不在二叉查找树中。这个剪枝大约可以节省30ms的时间,测试点4用时约160ms。
否则,可以使用set.count()判断查询点是否在树中,因为set采用红黑树作为载体结构,且题目保证所有节点的关键字值不相等,所以可以保证每次查询节点是否在树中的时候花费O(logn)的时间,但是在判断两个查询数值LCA的时候,仍然需要以原始树结构为标准。使用set结构后,测试点4用时约125ms。
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <set>
using namespace std;
const int maxn = 10010;
int pre[maxn];
set<int> tree;
struct Node {
public:
int d, visted;
Node* lchild, * rchild, *father;
};
Node* createNode(int data) {
Node* root = new Node;
root->d = data;
root->visted = 0;
root->lchild = root->father = root->rchild = NULL;
return root;
}
Node* createBST(int l, int r) {
if (l == r) {
return createNode(pre[l]);
}
int rootd = pre[l];
int rtrid = l + 1;
Node* root = createNode(rootd);
for (; rtrid <= r; rtrid++) {
if (pre[rtrid] > rootd) {
break;
}
}
if (rtrid != l + 1) {
root->lchild = createBST(l + 1, rtrid - 1);
root->lchild->father = root;
}
if (rtrid != r + 1) {
root->rchild = createBST(rtrid, r);
root->rchild->father = root;
}
return root;
}
int tagFind(int x, Node* root, Node*& last) {
if (root == NULL) {
return 0;
}
root->visted = 1;
last = root;
if (root->d == x) {
return 1;
}
else if (x < root->d) {
return tagFind(x, root->lchild, last);
}
else {
return tagFind(x, root->rchild, last);
}
}
int findLCA(int x, Node* root, int& ans) {
if (root == NULL) {
return 0;
}
if (root->visted) {
ans = root->d;
}
if (root->d == x) {
return 1;
}
else if (x < root->d) {
return findLCA(x, root->lchild, ans);
}
else {
return findLCA(x, root->rchild, ans);
}
}
int clearTag(Node* last) {
while (last != NULL) {
last->visted = 0;
last = last->father;
}
return 0;
}
int lca(int a, int b, Node* root) {
Node* last = NULL;
int ans = 0;
int flaga, flagb;
if (!tree.count(a)) {
flaga = 0;
}
else {
flaga = tagFind(a, root, last);
}
if (!tree.count(b)) {
flagb = 0;
}
else {
flagb = findLCA(b, root, ans);
}
clearTag(last);
if (flaga && flagb) {
if (ans == a) {
printf("%d is an ancestor of %d.\n", a, b);
}
else if (ans == b) {
printf("%d is an ancestor of %d.\n", b, a);
}
else {
printf("LCA of %d and %d is %d.\n", a, b, ans);
}
}
else if (!flaga && !flagb) {
printf("ERROR: %d and %d are not found.\n", a, b);
}
else if (!flaga && flagb) {
printf("ERROR: %d is not found.\n", a);
}
else if (flaga && !flagb) {
printf("ERROR: %d is not found.\n", b);
}
return 0;
}
int main()
{
int n, m, a, b;
scanf("%d%d", &m, &n);
for (int i = 0; i < n; i++) {
scanf("%d", &pre[i]);
tree.insert(pre[i]);
}
Node* root = createBST(0, n - 1);
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
lca(a, b, root);
}
return 0;
}