1143 Lowest Common Ancestor (30分) 附测试点分析

本文介绍了一种使用二叉查找树解决寻找两个节点最近公共祖先的问题。通过对树进行遍历和标记,可以有效地找到最低公共祖先。同时,文章讨论了在数据结构退化为链式结构时的查询效率,并提出了使用红黑树优化查询时间以及剪枝策略来提高性能。
摘要由CSDN通过智能技术生成

原题目参见: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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ProfSnail

谢谢老哥嗷

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值