PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000072. Đếm những gốc cho LCA cho trước (Root LCA Queries)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm những gốc cho LCA cho trước (Root LCA Queries)
Root LCA Queries
Source: CSAcademy
Version: Phuoc Hung OJ Extended
Problem Statement
Given an undirected tree of vertices, a query considers every possible vertex as the root and computes the LCA of and under root . Count how many root choices yield exactly vertex . Every qualifying root contributes once. Queries are independent and do not alter the tree.
Input
First line: , then edges , followed by queries .
Output
Print the number of valid root vertices for each query.
Subtasks
General constraints: ; ; và đôi một khác nhau; cạnh tạo cây.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 2
1 2
1 3
3 4
3 5
4 5 3
2 4 1
Output
3
1
Explanation
For , roots all produce LCA , giving choices. For only root qualifies.