#include<iostream> #include<vector> #include<map> usingnamespacestd; map<int ,bool> flag; map<int ,int > in ; vector<int> pre; intmain(){ int M ,N ,t ,t2; cin >> M >> N ; pre.resize(N+1); for(int i = 0 ; i <N ; i++) { cin >>t ; in[t] = i ; flag[t] = true ; } for(int i = 0 ; i< N ; i++) cin >>pre[i] ; for(int i = 0 ; i< M ;i++){ cin >> t>> t2 ; if(flag[t] && !flag[t2]) printf("ERROR: %d is not found.\n",t2); elseif(!flag[t] && flag[t2]) printf("ERROR: %d is not found.\n",t); elseif(!flag[t] && !flag[t2]) printf("ERROR: %d and %d are not found.\n",t,t2); else{ int c1 = in[t] , c2 =in[t2]; for(int j = 0 ; j< pre.size() ; j++){ int cj = in[pre[j]]; if(cj==c1){printf("%d is an ancestor of %d.\n",t,t2);break;} elseif(cj==c2){printf("%d is an ancestor of %d.\n",t2,t);break;} elseif((cj - c1)*(cj - c2) < 0){ printf("LCA of %d and %d is %d.\n",t,t2,pre[j]); break ; } } } } }