PAT_A1151#LCA in a Binary Tree
Source:
Description:
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 andA
is the key. But ifA
is one of U and V, printX is an ancestor of Y.
whereX
isA
andY
is the other node. If U or V is not found in the binary tree, print in a lineERROR: U is not found.
orERROR: V is not found.
orERROR: 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.
Keys:
Attention:
- 没有卡时间,所以常规操作就能通过,基本功要扎实;
- 更新了优化算法;
Code:
基本方法:
1 /* 2 Data: 2019-05-13 21:06:50 3 Problem: PAT_A1151#LCA in a Binary Tree 4 AC: 57:30 5 6 题目大意: 7 求U和V最近公共祖先 8 输入: 9 第一行给出,M测试组数<=1e3,N结点数<=1e4 10 接下来两行给出,中序遍历和先序遍历 11 接下来M行给出,给出查询结点U和V 12 输出: 13 如果U和V有公共祖先,LCA of U and V is A. 其中,A是key值 14 如果U是V的祖先,A is an ancestor of V. 其中,A是key值,V是结点序号 15 如果U或V未找到,ERROR: U is not found. 16 如果U和V均未找到,ERROR: U and V are not found. 17 */ 18 19 #include<cstdio> 20 #include<stack> 21 #include<map> 22 using namespace std; 23 const int M=1e4+10; 24 int in[M],pre[M],v1,v2,f1,f2; 25 stack<int> p1,p2; 26 map<int,int> key; 27 struct node 28 { 29 int data; 30 node *lchild,*rchild; 31 }; 32 33 node* Create(int preL,int preR,int inL,int inR) 34 { 35 if(preL > preR) 36 return NULL; 37 node *root = new node; 38 root->data = pre[preL]; 39 int k; 40 for(k=inL; k<=inR; k++) 41 if(in[k]==root->data) 42 break; 43 int numLeft = k-inL; 44 root->lchild = Create(preL+1,preL+numLeft,inL,k-1); 45 root->rchild = Create(preL+numLeft+1,preR,k+1,inR); 46 return root; 47 } 48 49 void DFS(node *root) 50 { 51 if(!root) 52 return; 53 if(f1) p1.push(root->data); 54 if(f2) p2.push(root->data); 55 if(root->data==v1) f1=0; 56 if(root->data==v2) f2=0; 57 DFS(root->lchild); 58 DFS(root->rchild); 59 if(f1) p1.pop(); 60 if(f2) p2.pop(); 61 } 62 63 int main() 64 { 65 #ifdef ONLINE_JUDGE 66 #else 67 freopen("Test.txt", "r", stdin); 68 #endif 69 70 int n,m; 71 scanf("%d%d", &m,&n); 72 for(int i=0; i<n; i++) 73 scanf("%d", &in[i]); 74 for(int i=0; i<n; i++) 75 { 76 scanf("%d", &pre[i]); 77 key[pre[i]]=1; 78 } 79 node *root = Create(0,n-1,0,n-1); 80 for(int i=0; i<m; i++) 81 { 82 scanf("%d%d",&v1,&v2); 83 if(key[v1]==0 && key[v2]==0) 84 printf("ERROR: %d and %d are not found.\n", v1,v2); 85 else if(key[v1]==0 || key[v2]==0) 86 printf("ERROR: %d is not found.\n", key[v2]==0?v2:v1); 87 else 88 { 89 while(!p1.empty()) p1.pop(); 90 while(!p2.empty()) p2.pop(); 91 f1=1;f2=1; 92 DFS(root); 93 while(p1.size() > p2.size()) p1.pop(); 94 while(p2.size() > p1.size()) p2.pop(); 95 while(p1.top() != p2.top()) 96 {p1.pop();p2.pop();} 97 if(p1.top()!=v1 && p1.top()!=v2) 98 printf("LCA of %d and %d is %d.\n",v1,v2,p1.top()); 99 else if(p1.top() == v1) 100 printf("%d is an ancestor of %d.\n", v1,v2); 101 else 102 printf("%d is an ancestor of %d.\n", v2,v1); 103 } 104 } 105 106 return 0; 107 }
优化算法:
1 /* 2 Data: 2019-06-29 15:55:25 3 Problem: PAT_A1151#LCA in a Binary Tree 4 AC: 38:24 5 6 题目大意: 7 求U和V最近公共祖先 8 输入: 9 第一行给出,M测试组数<=1e3,N结点数<=1e4 10 接下来两行给出,中序遍历和先序遍历 11 接下来M行给出,给出查询结点U和V 12 输出: 13 如果U和V有公共祖先,LCA of U and V is A. 其中,A是key值 14 如果U是V的祖先,A is an ancestor of V. 其中,A是key值,V是结点序号 15 如果U或V未找到,ERROR: U is not found. 16 如果U和V均未找到,ERROR: U and V are not found. 17 18 基本思路: 19 若U和V存在,根据先序序列依次遍历根结点,设为root 20 中序遍历中,若U和V分别在root的两端,则说明该结点为公共祖先(显然) 21 且该结点一定是最近公共祖先(存在且唯一) 22 证明: 23 若roo1是U和V的最近公共祖先,存在root2是U和V的非最近公共祖先 24 则root1位于root2的左子树或右子树(或者说roo1位于root2至U和V的路径上), 25 而U和V是root1的子树 26 故U和V位于root2的左子树或右子树 27 故有且仅有最近公共祖先,位于U和V的中间 28 */ 29 #include<cstdio> 30 #include<unordered_map> 31 using namespace std; 32 const int M=1e4+10; 33 int in[M],pr[M]; 34 unordered_map<int,int> pin,ppr; 35 int n,m,v1,v2; 36 37 void LCA(int prL, int prR, int inL, int inR) 38 { 39 if(prL > prR) 40 return; 41 int rt=pr[prL]; 42 int numLeft=pin[rt]-inL; 43 if(rt == v1) 44 { 45 printf("%d is an ancestor of %d.\n", rt,v2); 46 return; 47 } 48 else if(rt == v2) 49 { 50 printf("%d is an ancestor of %d.\n", rt,v1); 51 return; 52 } 53 else if(pin[v1]<pin[rt] && pin[v2]<pin[rt]) 54 LCA(prL+1,prL+numLeft,inL,pin[rt]-1); 55 else if(pin[v1]>pin[rt] && pin[v2]>pin[rt]) 56 LCA(prL+numLeft+1,prR,pin[rt]+1,inR); 57 else 58 printf("LCA of %d and %d is %d.\n", v1,v2,rt); 59 } 60 61 int main() 62 { 63 #ifdef ONLINE_JUDGE 64 #else 65 freopen("Test.txt", "r", stdin); 66 #endif // ONLINE_JUDGE 67 68 scanf("%d%d", &m,&n); 69 for(int i=1; i<=n; i++) 70 { 71 scanf("%d", &in[i]); 72 pin[in[i]]=i; 73 74 } 75 for(int i=1; i<=n; i++) 76 { 77 scanf("%d", &pr[i]); 78 ppr[pr[i]]=i; 79 } 80 while(m--) 81 { 82 scanf("%d%d", &v1,&v2); 83 if(pin[v1]==0 && pin[v2]==0) 84 printf("ERROR: %d and %d are not found.\n", v1,v2); 85 else if(pin[v1]==0) 86 printf("ERROR: %d is not found.\n", v1); 87 else if(pin[v2]==0) 88 printf("ERROR: %d is not found.\n", v2); 89 else 90 LCA(1,n,1,n); 91 } 92 93 94 return 0; 95 }