PAT A1143 Lowest Common Ancestor

PAT A1143 Lowest Common Ancestor

在这里插入图片描述

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.
  • 分析:
    LCA:最近公共祖先,
  1. u、v互为祖先关系:若u是v的祖先,则u是v的LCA
  2. 否则:LCA为两个节点u、v向上查找到的第一个公共祖先c(c一定满足使u、v分别位于他们两侧,若在一侧,要么有更优的LCA,要么一个是另一个的祖先)
  • 思路 1:
    首先判断是不是树的节点:使用map来标记所有树的节点

1- :a.b都是树的节点->找ACL:
对于先序遍历:每次都是先取一个子树的根,所以顺着先序序列,不断判断u和v是否位于c的两侧:
1-1: 如果c是u、v中的一个(比如u),那么u就是v的祖先
1-2: 如果c不是u、v中的一个:第一次匹配的元素就是u和v的LCA,两个节点分别位于c的左右子树,他们向上汇聚,最先只能到c(因为,之前的元素显然不是[u、v到之前的节点必途径c,c更优],之后的元素,因为u、v分别位于c的左右子树上,按先序序列c之后的节点只能位于c的其中一棵子树上,不可能是两个节点的公共祖先)
2- :a.b只有一个是树的节点:输出不是的那个:x is not found
3- :a.b都不是:a&b are not found

  • code 1:
#include <iostream>
#include <unordered_map>
using namespace std;
const int maxn = 10010;
int main(){
	int n, m, pre[maxn], a, b;
	unordered_map<int, bool> has;
	scanf("%d %d", &m, &n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &pre[i]);
		has[pre[i]] = true;
	}
	for(int i = 0; i < m; ++i){
		scanf("%d %d", &a, &b);
		if(has[a] * has[b] == 1){	//1- 两个节点都存在 
			for(int j = 0; j < n; ++j){
				if((a <= pre[j] && b >= pre[j]) || ((a >= pre[j] && b <= pre[j]))){
					if(a == pre[j] || b == pre[j])	//1-1:a/b其中一个为另一个的祖先 
						printf("%d is an ancestor of %d.\n", a == pre[j] ? a : b, a == pre[j] ? b : a);
					else printf("LCA of %d and %d is %d.\n", a, b, pre[j]);	//1-2:找到ACL 
					break;
				}
			}
		}else if(has[a] != has[b]){	//2- 只有一个存在 
			printf("ERROR: %d is not found.\n", has[a] ? b : a);
		}else printf("ERROR: %d and %d are not found.\n", a, b); //3- 两个都不存在 
	}
	return 0;
}
  • TIPS 1: map输出不存在的元素时返回0
mark[1] = true;
cout << mark[1] << " " << mark[0];	//输出结果1 0 
  • T2 code: 同样的思路
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
int pre[maxn];
unordered_map<int, bool> has;

void Create(int preL, int preR, int q1, int q2){
	while(1){
		int r = pre[preL], Min = min(q1, q2), Max = max(q1, q2);
		if(r == q1 || r == q2){
			printf("%d is an ancestor of %d.\n", r, r == q1 ? q2 : q1);
			break;
		}else if(Min < r && r < Max){
			printf("LCA of %d and %d is %d.\n", q1, q2, r);			
			break;
		}
//		int id = upper_bound(pre + preL + 1, pre + preR, r) - pre;	//TIPS 1不需要左右树找,每个节点作为根试一下就可以了
//		if(r > Max){
			preL++;
//		}
//		else if(r < Min){
//			preL = id;
//		}
	} 

}
int main(){
	int m, n;
	scanf("%d %d", &m, &n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &pre[i]);
		has[pre[i]] = true;
	}
	for(int i = 0; i < m; ++i){
		int q1, q2;
		scanf("%d %d", &q1, &q2);
		if(has[q1] && has[q2]){
			Create(0, n - 1, q1, q2);
		}else if(has[q1] || has[q2]) printf("ERROR: %d is not found.\n", has[q1] ? q2 :q1 );
		else printf("ERROR: %d and %d are not found.\n", q1, q2);
	}
	return 0;
}

  • 思路 2: 建树,先序找: 有点脱裤子放屁的意思,本来就给了先序序列,不需要重新建树再先序遍历

  • T2 code:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
int pre[maxn];
unordered_map<int, bool> has;
struct node{
	int data;
	node *lc ,*rc;
};
node* NewNode(int x){
	node* r = new node;
	r->data = x;
	r->lc = r->rc = NULL;
	return r;
}
void Create(node* & r, int preL, int preR){
	if(preL > preR) return;
	r = NewNode(pre[preL]);
	int id = preL + 1;
	while(id < preR && pre[id] < pre[preL]) id++;
	Create(r->lc, preL + 1, id - 1); 
	Create(r->rc, id, preR);
}
bool flg;
void PreOrder(node* r, int a, int b){
	if(flg){
		if(r->data == a || r->data == b){
			printf("%d is an ancestor of %d.\n", r->data, r->data == a ? b : a);
			flg = false;
		}else if(r->data < b && r->data > a || r->data < a && r->data > b){
			printf("LCA of %d and %d is %d.\n", a, b, r->data);
			flg = false;
		}
	}
	if(r->lc != NULL) PreOrder(r->lc, a, b);
	if(r->rc != NULL) PreOrder(r->rc, a, b);
}
int main(){
	int m, n;
	scanf("%d %d", &m, &n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &pre[i]);
		has[pre[i]] = true;
	}
	node* r;
	Create(r, 0, n - 1);
	for(int i = 0; i < m; ++i){
		int q1, q2;
		scanf("%d %d", &q1, &q2);
		if(has[q1] && has[q2]){
			flg = true;
			PreOrder(r, q1, q2);
		}else if(has[q1] || has[q2]) printf("ERROR: %d is not found.\n", has[q1] ? q2 :q1 );
		else printf("ERROR: %d and %d are not found.\n", q1, q2);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值