PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000070. LCA với gốc thay đổi (Dynamic-Root LCA)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
LCA với gốc thay đổi (Dynamic-Root LCA)
Dynamic-Root LCA
Source: VNOJ
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree has a current root, initially vertex . Two commands are supported: ! r sets the current root to uv$ under the current root. Root changes persist until replaced by another ! command. No edge ever changes.
Input
First line is , followed by edges and number of commands . Each of the next lines is ! r or ? u v.
Output
For each ? command print one line containing the requested LCA vertex.
Subtasks
General constraints: ; ; ; đồ thị là cây.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
9
1 2
1 3
2 4
2 5
3 6
3 7
6 8
6 9
5
? 4 5
? 5 6
? 8 7
! 6
? 8 7
Output
2
1
3
6
Explanation
Before rerooting, . After ! 6, the root-to- and root-to- paths share vertex as their deepest common ancestor.