Loading [MathJax]/jax/output/CommonHTML/jax.js
World tree - MarisaOJ: Marisa Online Judge

World tree

Time limit: 1500 ms
Memory limit: 128 MB
Given a tree graph with N vertices and Q queries. The i-th query consists of mi special vertices x1,x2,x3,…xmi. A vertex u on the tree is considered to be occupied by a special vertex x if the distance from u to x is the smallest among the mi special vertices (if the distance from u to x1 is equal to the distance from u to x2 and x1<x2, then u will preferentially be occupied by x1). For each query, print the number of vertices that each special vertex xi occupies. ### Input - The first line contains an integer N. - The next N−1 lines describe the edges of the tree. - The following line contains an integer Q. - The next Q×2 lines are structured as follows: - The first line contains an integer mi. - The next line contains mi integers x1,x2,…, which are the special vertices. ### Output - Output the answer for each query in the order of the input. ### Constraints - N,Q≤500000. - mi,xi≤n. - ∑Qi=1mi ≤500000 ### Example Input: 1021324354617383941011487103 Output: 1135
Topic
Tree
Dynamic Programming
Rating 2000
Source HNOI
Solution (0) Solution