#LCA0000073. Phục hưng — LCA theo đoạn và gốc (Regnaissance)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Phục hưng — LCA theo đoạn và gốc (Regnaissance)
Regnaissance
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree has vertices labeled through . For each query , temporarily root the tree at and compute the LCA of all vertices whose labels belong to , endpoints included. The root choice applies only to that query. The label interval does not refer to DFS order. The tree edges never change.
Input
First line: , then edges , then queries .
Output
For each query print the LCA vertex label under root .
Subtasks
General constraints: ; ; ; đầu vào là cây.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 6
1 2
1 3
3 4
3 5
1 3 2
1 1 2
2 3 2
4 5 4
2 5 3
4 5 5
Output
2
1
2
4
3
5
Explanation
For the query set contains only vertex , so its LCA is regardless of root. For the root is itself included, and is the LCA.