PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000094. Tổ tiên chung gần nhất (Nearest Common Ancestors)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổ tiên chung gần nhất (Nearest Common Ancestors)

Nearest Common Ancestors

Source: POJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a rooted tree of nn vertices numbered 11 through nn. Every edge (u,v)(u,v) indicates that uu is the direct parent of vv. The root is the unique vertex without a parent. For two distinct vertices x,yx,y, find their lowest common ancestor: the deepest vertex lying on both root-to-vertex paths. A vertex is considered its own ancestor.

Input

The first line contains nn. Each of the next n−1n-1 lines gives a parent–child pair u,vu,v. The last line contains x,yx,y.

Output

Print the lowest common ancestor of xx and yy.

Subtasks

  • Subtask 1 (20 points): 2≤n≤352\le n\le 35; n−1n-1 edges form a rooted tree, 1≤x,y≤n1\le x,y\le n, x≠yx\ne y.
  • Subtask 2 (30 points): 2≤n≤3002\le n\le 300; n−1n-1 edges form a rooted tree, 1≤x,y≤n1\le x,y\le n, x≠yx\ne y.
  • Subtask 3 (50 points): 2≤n≤1042\le n\le 10^4; n−1n-1 edges form a rooted tree, 1≤x,y≤n1\le x,y\le n, x≠yx\ne y.

Examples

Input

5
2 3
3 4
3 1
1 5
3 5

Output

3

Explanation

Vertex 33 is the lowest common ancestor of vertices 33 and 55.