#LCA0000077. Tổ tiên chung thấp nhất bậc k (K-th LNCA)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổ tiên chung thấp nhất bậc k (K-th LNCA)
K-th LNCA
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A tree is rooted at vertex . Each query gives and a set of distinct vertices. Consider every size- subset , compute its lowest common ancestor, and collect these results into a set (duplicates removed). A member of is a k-th LNCA if its depth equals the maximum depth of any vertex in . Count the distinct k-th LNCA vertices. This is not the ordinary -th ancestor operation.
Input
First line contains , followed by edges . Each query is .
Output
Print the number of distinct maximum-depth LNCA vertices per query.
Subtasks
General constraints: ; ; ; các đỉnh trong mỗi truy vấn phân biệt.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
7 1
1 2
1 3
2 4
2 5
3 6
3 7
2 4 4 5 6 7
Output
2
Explanation
Pairs and have LCAs and of depth . Cross-branch pairs have LCA of depth . The deepest candidates are and , so the answer is .