forked from SoumyadeepMukherjee/Advanced-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lca.cpp
62 lines (60 loc) · 1.03 KB
/
lca.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> t[N];
int p[N];
void dfs(int v, int par = -1)
{
p[v] = par;
for (auto child : t[v])
{
if (child == par)
{
continue;
}
dfs(child, v);
}
}
vector<int> path(int v)
{
vector<int> ans;
while (v != -1)
{
ans.push_back(v);
v = p[v];
}
reverse(ans.begin(), ans.end());
return ans;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int x, y;
cin >> x >> y;
t[x].push_back(y);
t[y].push_back(x);
}
dfs(1);
int x, y;
cin >> x >> y;
vector<int> path_x = path(x);
vector<int> path_y = path(y);
int lca = -1;
int max_size = min(path_y.size(), path_x.size());
for (int i = 0; i < max_size; i++)
{
if (path_x[i] == path_y[i])
{
lca = path_x[i];
}
else
{
break;
}
}
cout << lca << endl;
return 0;
}