Tree in Tree
Time limit6sMemory limit512 MB
For each vertex subset, count the edges in its minimal connecting subtree using Euler tour order and LCA checks.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Sorting, Binary search
- Solved
- No attempts yet
Problem
A tree is given. A tree is an undirected and connected graph with vertices and edges. The vertices are labeled .
There are queries. In each query we choose a subset of the tree's vertices and ask how many edges lie in the minimum spanning tree of the set . In other words, how many edges belong to at least one path between some vertices of the set ? Find the answers to all queries.
Input
The first line contains a positive integer (), the number of the tree's vertices.
Each of the next lines contains two distinct positive integers () which mean that vertices and are connected by an edge.
The next line contains a positive integer (), the number of queries.
Each of the next lines contains first a positive integer (), the size of the set, and then distinct positive integers between and which are the vertices of the set from the problem statement.
The sum of the sizes over all queries is at most .
Output
For each query, print the required number of edges on a separate line.