1 #include<bits/stdc++.h>
2 using namespace std;
3 #define maxn 1000000
4 int n,x,y,head[maxn],num;
5 int deep[maxn],size[maxn],dad[maxn],top[maxn];
6
7 struct Edge{
8 int v,next;
9 }edge[maxn];
10
11 void add(int u,int v)
12 {
13 edge[++num].v=v;
14 edge[num].next=head[u];
15 head[u]=num;
16 }
17
18 void dfs(int x)
19 {
20 size[x]=1 ; deep[x]=deep[dad[x]]+1;
21 for(int i=head[x];i;i=edge[i].next)
22 {
23 int v=edge[i].v;
24 if(dad[x]==v) continue;
25 dad[v]=x; dfs(v); size[x]+=size[v];
26 }
27 }
28
29 void dfs2(int x)
30 {
31 int t=0; if(!top[x]) top[x]=x;
32 for(int i=head[x];i;i=edge[i].next)
33 {
34 int v=edge[i].v;
35 if(dad[x]!=v&&size[t]<size[v]) t=v;
36 }
37 if(t) top[t]=top[x],dfs2(t);
38 for(int i=head[x];i;i=edge[i].next)
39 {
40 int v=edge[i].v;
41 if(dad[x]!=v&&t!=v) dfs2(v);
42 }
43 }
44
45 int lca(int x,int y)
46 {
47 for(;top[x]!=top[y];x=dad[top[x]])
48 if(deep[top[x]]<deep[top[y]]) swap(x,y);
49 return deep[x]<deep[y]?x:y;
50 }
51
52
53 int main()
54 {
55 scanf("%d",&n);
56 for(int i=1;i<n;i++)
57 {
58 scanf("%d%d",&x,&y);
59 add(x,y); add(y,x);
60 }
61 dfs(1); dfs2(1);
62 int a,b,ans;
63 scanf("%d%d",&a,&b);
64 printf("%d",lca(a,b));
65 return 0;
66 }