Problem C
Rooted Subtrees
A tree is a connected, acyclic, undirected graph
with
Let
You are given
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.
Input
The first line contains two space-separated integers
Each of the next
Each of the next
Output
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.
Sample Explanation
The possible rootings of the first tree are
![\includegraphics[width=0.90\textwidth ]{trees.png}](/problems/rootedsubtrees/file/statement/en/img-0001.png)
Considering the rootings at
-
by choosing , , -
by choosing , , -
by choosing , , -
by choosing , , -
by choosing , , -
by choosing , , -
by choosing , , -
and
by choosing , .
If the rootings are instead at
-
by choosing , , -
by choosing , , -
by choosing , , -
by choosing , , -
by choosing , , -
and
by choosing , .
For some of these, there are other ways to choose
Sample Input 1 | Sample Output 1 |
---|---|
5 2 1 2 2 3 2 4 4 5 1 3 4 5 |
8 6 |