1143. Lowest Common Ancestor (30)-PAT甲级真题 (LCA 二分法 构造树)

题意

给定排序树的结点数量n,再给出排序树的先序遍历序列。再询问m次,每次给出两个结点s、e,首先判断是否存在,然后找两个点的最近公共祖先LCA。

思路1 - 对应解法1(AC)

  • 先用数组a接收输入的先序遍历序列,通过哈希表rec记录节点的存在位置rec[a[i]] = i;
  • 再循环m次:
    • 每次接收两个节点s e,通过rec先确定是否存在。不存在时输出进入下层循环。
    • 由先序序列和LCA特性,最近公共祖先是遍历序列时的第一个具备如下性质的数字:(tar - s) * (tar - e) == 0, (开始把性质推错了,结果在min(rec[s], rec[e])开始从右往左遍历,卡了好久,阿巴阿巴)

思路2 - 对应解法2(测试点4 - WA)

  • 第一步同思路1
  • 然后常规造树,建了一棵静态链表,pp数组指向节点的父亲。
  • 再循环m次:
    • 同思路1先确定是否存在。
    • 然后通过mp、mp2两个哈希表分别记录s、e到根结点的路径上出现过的节点。
    • 如果e出现在s中说明e是s的祖先,反之同理。
    • 然后再遍历一趟s到根结点的路径,第一个出现在mp2中的结点是LCA。
  • 到这里写完后提交。测试点4、测试点5都TLE了,思考是建树的时候找右子树起始位置il的时间复杂度为O(n),于是改用二分查找,果不其然写了很长时间,要多回顾。找右侧性质a[m] >= a[l]的最左端应该是r = m, 对应于m = l + r >> 1;
int il = l + 1, ir = r;// 目的找第一个大于等于a[l]的位置 
while(il < ir){//出来时il == ir (初始也可能il == ir + 1直接跳出来) 
	int m = il + ir >> 1; 
	if(a[m] < a[l]) il = m + 1;
	else ir = m;
}
  • 改正后测试点5仍然TLE,同时测试点4WA:分析测试点4是因为多个相同数字出现,测试点5是树太过不平衡。
  • 做到1151时换了种做法,把记录路径的哈希表改为数组,测试点5就过了,但是测试点4仍然WA。这里注意哈希表的插入速度是 O ( l o g 2 n ) O(log_2n) O(log2n)慢于数组头插O(1)

总结

  • LCA特性,最近公共祖先是遍历序列时的第一个具备如下性质的数字:(tar - s) * (tar - e) == 0
  • 哈希表的插入速度是 O ( l o g 2 n ) O(log_2n) O(log2n)慢于数组头插O(1)。
  • 能找到数学规律并用其解决问题的,就没必要用常规方法。

解法1 - (AC)

#include<bits/stdc++.h>
using namespace std;
int n, m, s, e, ptr, a[10001];
unordered_map<int, int> rec;
int main(){
	scanf("%d %d", &m, &n);
	for(int i = 1; i <= n; i ++){
		scanf("%d", &a[i]);
		rec[a[i]] = i;
	}
	while(m--){
		scanf("%d %d", &s, &e);
		int as = rec[s], ae = rec[e];
		if(as == 0 && ae == 0) printf("ERROR: %d and %d are not found.\n", s, e);
		else if(as * ae == 0) printf("ERROR: %d is not found.\n", as ? e : s);
		else{
			for(ptr = 1; ; ptr++)
				if((a[ptr] - s) * (a[ptr] - e) <= 0) break;
			if(a[ptr] == s || a[ptr] == e) 
				printf("%d is an ancestor of %d.\n", a[ptr], a[ptr] == s ? e : s);
			else printf("LCA of %d and %d is %d.\n", s, e, a[ptr]);
		} 
	}
	return 0;
} 

解法2 - 建树 - (测试点4WA)

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, s, e;
int a[N], pp[N];
void mt(int l, int r, int p){
	if(l > r) return;
	pp[l] = p;
	if(l == r) return;
	/* 注释1
	int i = l + 1;
	while(i <= r && a[i] < a[l]) i++;
	mt(l + 1, i - 1, l);
	mt(i, r, l);
	*/
	int il = l + 1, ir = r;// 目的找第一个大于等于a[l]的位置 
	while(il < ir){//出来时il == ir (初始也可能il == ir + 1直接跳出来) 
		int m = il + ir >> 1; 
		if(a[m] < a[l]) il = m + 1;
		else ir = m;
	}
	mt(l + 1, il - 1, l);
	mt(il, r, l);
}
int main(){
	cin>>m>>n;
	unordered_map<int, int> rec;
	for(int i = 1; i <= n; i ++){
		scanf("%d", &a[i]);
		rec[a[i]] = i;
	}
	mt(1, n, 0);
	while(m--){
		scanf("%d %d", &s, &e);
		int as = rec[s], ae = rec[e];
		if(as == 0 && ae == 0) printf("ERROR: %d and %d are not found.\n", s, e);
		else if(as * ae == 0) printf("ERROR: %d is not found.\n", as == 0 ? s : e);
		else{
			deque<int> rec1, rec2;
			int start = as, ptr = 0;
			while(start != 0){
				rec1.push_front(a[start]);
				start = pp[start];
			}
			start = ae;
			while(start != 0){
				rec2.push_front(a[start]);
				start = pp[start];
			}
			while(ptr < rec1.size() && ptr < rec2.size() && rec1[ptr] == rec2[ptr]) ptr++;
			if(rec1[ptr - 1] == s) printf("%d is an ancestor of %d.\n", s, e);
			else if(rec1[ptr - 1] == e)printf("%d is an ancestor of %d.\n", e, s);
			else printf("LCA of %d and %d is %d.\n", s, e, rec1[ptr - 1]);
		/*注释2
			unordered_map<int, bool> mp, mp2;
			int start = as;
			while(start != 0){
				mp[start] = true;
				start = pp[start];
			}
			start = ae;
			while(start != 0){
				mp2[start] = true;
				start = pp[start];
			}
			if(mp[ae]) printf("%d is an ancestor of %d.\n", e, s);
			else if(mp2[as])printf("%d is an ancestor of %d.\n", s, e);
			else{
				start = as;
				while(start != 0){
					start = pp[start];
					if(mp2[start]){
						printf("LCA of %d and %d is %d.\n", s, e, a[start]);
						break;
					}
				}
			}
		}
		*/
	}
	return 0;
}

题目

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.

A binary search tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
  • Both the left and right subtrees must also be binary search trees.

Given any two nodes in a BST, you are supposed to find their LCA.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.

Output Specification:

For each given pair of U and V, print in a line LCA of U and V is A. if the LCA is found and A is the key. But if A is one of U and V, print X is an ancestor of Y. where X is A and Y is the other node. If U or V is not found in the BST, print in a line ERROR: U is not found. or ERROR: V is not found. or ERROR: U and V are not found..

Sample Input:

6 8
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99

Sample Output:

LCA of 2 and 5 is 3.
8 is an ancestor of 7.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值