#P16005. [ICPC 2020 NAC] Rooted Subtrees
[ICPC 2020 NAC] Rooted Subtrees
题目描述
A tree is a connected, acyclic, undirected graph with nodes and edges. There is exactly one path between any pair of nodes. A rooted tree is a tree with a particular node selected as the root.
Let be a tree and be that tree rooted at node . The subtree of in is the set of all nodes where the path from to contains (including itself). In this problem, we denote the set of nodes in the subtree of in the tree rooted at as .
You are given queries. Each query consists of two (not necessarily different) nodes, and . A set is “obtainable” if and only if it can be expressed as the intersection of a subtree in the tree rooted at and a subtree in the tree rooted at . Formally, a set is “obtainable” if and only if there exist nodes and where .
For a given pair of roots, count the number of different non-empty obtainable sets. Two sets are different if and only if there is an element that appears in one, but not the other.
输入格式
The first line contains two space-separated integers and (), where is the number of nodes in the tree and is the number of queries to be answered. The nodes are numbered from to .
Each of the next lines contains two space-separated integers and (, ), indicating an undirected edge between nodes and . It is guaranteed that this set of edges forms a valid tree.
Each of the next lines contains two space-separated integers and (), which are the nodes of the roots for the given query.
输出格式
For each query output a single integer, which is the number of distinct obtainable sets of nodes that can be generated by the above procedure.
5 2
1 2
2 3
2 4
4 5
1 3
4 5
8
6
提示
Sample Explanation
The possible rootings of the first tree are
:::align{center}
:::
Considering the rootings at and , the obtainable sets are:
- by choosing ,
- by choosing ,
- by choosing ,
- by choosing ,
- by choosing ,
- by choosing ,
- by choosing ,
- and by choosing .
If the rootings are instead at and , there are only obtainable sets:
- by choosing ,
- by choosing ,
- by choosing ,
- by choosing ,
- by choosing ,
- and by choosing .
For some of these, there are other ways to choose and to arrive at the same set.