题意
给定排序树的结点数量n,再给出排序树的先序遍历序列、中序遍历序列。再询问m次,每次给出两个结点s、e,首先判断是否存在,然后找两个点的最近公共祖先LCA。
思路
参考1143,基本没啥变化,这里构造树用的三叉静态链表。
解法2是常规方法,也是造树、遍历。
解法1
#include<bits/stdc++.h>
using namespace std;
const int N = 10010;
int n, m, s, e, p[N], a[N], pre[N], in[N], idx = 1;
struct node{
int dat, l, r, p;
}rec[N];
unordered_map<int, int> mp;
int mt(int pl, int pr, int il, int ir, int p){
if(pl > pr) return 0;
int ptr = idx++, i = il;
while(in[i] != pre[pl]) i++;
mp[pre[pl]] = ptr;
rec[ptr].dat = pre[pl];
rec[ptr].l = mt(pl + 1, i - il + pl, il, i - 1, ptr);
rec[ptr].r = mt(pr - ir + i + 1, pr,i + 1, ir, ptr);
rec[ptr].p = p;
return ptr;
}
int main(){
cin>>m>>n;
for(int i = 1; i <= n; i ++) scanf("%d", &in[i]);
for(int i = 1; i <= n; i ++) scanf("%d", &pre[i]);
mt(1, n, 1, n, 0);
while(m--){
scanf("%d %d", &s, &e);
int as = mp[s], ae = mp[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{
int start = as, ptr = 0;
deque<int> rec1, rec2;
while(start != 0){
rec1.push_front(rec[start].dat);
start = rec[start].p;
}
start = ae;
while(start != 0){
rec2.push_front(rec[start].dat);
start = rec[start].p;
}
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]);
}
}
return 0;
}
解法2
#include<bits/stdc++.h>
using namespace std;
struct node{
int k;
struct node *l,*r;
};
unordered_map<int,bool>exist;
vector<int>in,pre,rec1,rec2,rec;
int m,n,v1,v2;
node*BuildTree(int p_b,int p_e,int i_b,int i_e){
if(i_e<=i_b)return NULL;
node*x=new node();
x->k=pre[p_b];
int ptr=i_b;
for(;in[ptr]!=pre[p_b];ptr++);
x->l=BuildTree(p_b+1,ptr+1+p_b-i_b,i_b,ptr);
x->r=BuildTree(p_e-i_e+ptr+1,p_e,ptr+1,i_e);
return x;
}
void Preorder(node*s){
if(s==NULL)return;
rec.push_back(s->k);
if(s->k==v1)rec1=rec;
if(s->k==v2)rec2=rec;
Preorder(s->l);
Preorder(s->r);
rec.pop_back();
}
int main(){
cin>>m>>n;
in.resize(n),pre.resize(n);
for(int i=0;i<n;i++){
cin>>in[i];
exist[in[i]]=true;
}
for(int i=0;i<n;i++)cin>>pre[i];
node*root=BuildTree(0,n,0,n);
for(int i=0;i<m;i++){
cin>>v1>>v2;
if(!exist[v1]&&!exist[v2]) printf("ERROR: %d and %d are not found.\n",v1,v2);
else if(!exist[v1]) printf("ERROR: %d is not found.\n",v1);
else if(!exist[v2]) printf("ERROR: %d is not found.\n",v2);
else{
rec.clear();
rec.push_back(root->k);
Preorder(root);
int p=0;
while(rec1[p]==rec2[p]&&p<rec1.size()&&p<rec2.size())p++;
p--;
if(p<rec1.size()-1&&p<rec2.size()-1) printf("LCA of %d and %d is %d.\n",v1,v2,rec1[p]);
else if(p==rec1.size()-1) printf("%d is an ancestor of %d.\n",v1,v2);
else if(p==rec2.size()-1) printf("%d is an ancestor of %d.\n",v2,v1);
}
}
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.
Given any two nodes in a binary tree, 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 binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. 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 binary tree, 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
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99
Sample Output:
LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.