-
Notifications
You must be signed in to change notification settings - Fork 0
/
lck.cpp
46 lines (40 loc) · 944 Bytes
/
lck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<vector>
#include<map>
using namespace std;
map<int, bool> mp;
int main(){
int p, n;
cin>>p>>n;
vector<int> pre(n);
for(int i=0;i<n;i++){
cin>>pre[i];
mp[pre[i]] = true;
}
for (int i = 0; i < p; i++)
{
int u,v,a;
cin>>u>>v;
for (int j = 0; j < n; j++)
{
a = pre[j];
if((u<=a&&v>=a) || (u>=a&&v<=a) ){
break;
}
}
if(mp[u]==false&&mp[v]==false){
printf("ERROR: %d and %d are not found.\n", u, v);
}
else if (mp[u]==false || mp[v]==false)
{
printf("ERROR: %d are not found.\n", mp[u]==false?u:v);
}
else if(a==u || v==a){
printf("%d is an ancestor of %d.\n", a, a==u?v:u);
}
else{
printf("LCA of %d and %d is %d.\n", u, v, a);
}
}
return 0;
}