#P16005. [ICPC 2020 NAC] Rooted Subtrees

[ICPC 2020 NAC] Rooted Subtrees

题目描述

A tree is a connected, acyclic, undirected graph with nn nodes and n1n - 1 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 TT be a tree and TrT_r be that tree rooted at node rr. The subtree of uu in TrT_r is the set of all nodes vv where the path from rr to vv contains uu (including uu itself). In this problem, we denote the set of nodes in the subtree of uu in the tree rooted at rr as Tr(u)T_r(u).

You are given qq queries. Each query consists of two (not necessarily different) nodes, rr and pp. A set SS is “obtainable” if and only if it can be expressed as the intersection of a subtree in the tree rooted at rr and a subtree in the tree rooted at pp. Formally, a set SS is “obtainable” if and only if there exist nodes uu and vv where S=Tr(u)Tp(v)S = T_r(u) \cap T_p(v).

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 nn and qq (1n,q21051 \le n, q \le 2 \cdot 10^5), where nn is the number of nodes in the tree and qq is the number of queries to be answered. The nodes are numbered from 11 to nn.

Each of the next n1n - 1 lines contains two space-separated integers uu and vv (1u,vn1 \le u, v \le n, uvu \ne v), indicating an undirected edge between nodes uu and vv. It is guaranteed that this set of edges forms a valid tree.

Each of the next qq lines contains two space-separated integers rr and pp (1r,pn1 \le r, p \le n), 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 11 and 33, the 88 obtainable sets are:

  1. {1}\{1\} by choosing u=1,v=1u = 1, v = 1,
  2. {1,2,4,5}\{1, 2, 4, 5\} by choosing u=1,v=2u = 1, v = 2,
  3. {1,2,3,4,5}\{1, 2, 3, 4, 5\} by choosing u=1,v=3u = 1, v = 3,
  4. {2,3,4,5}\{2, 3, 4, 5\} by choosing u=2,v=3u = 2, v = 3,
  5. {2,4,5}\{2, 4, 5\} by choosing u=2,v=2u = 2, v = 2,
  6. {3}\{3\} by choosing u=3,v=3u = 3, v = 3,
  7. {4,5}\{4, 5\} by choosing u=2,v=4u = 2, v = 4,
  8. and {5}\{5\} by choosing u=5,v=5u = 5, v = 5.

If the rootings are instead at 44 and 55, there are only 66 obtainable sets:

  1. {1}\{1\} by choosing u=1,v=1u = 1, v = 1,
  2. {1,2,3}\{1, 2, 3\} by choosing u=2,v=4u = 2, v = 4,
  3. {1,2,3,4}\{1, 2, 3, 4\} by choosing u=4,v=4u = 4, v = 4,
  4. {1,2,3,4,5}\{1, 2, 3, 4, 5\} by choosing u=4,v=5u = 4, v = 5,
  5. {3}\{3\} by choosing u=3,v=2u = 3, v = 2,
  6. and {5}\{5\} by choosing u=5,v=5u = 5, v = 5.

For some of these, there are other ways to choose uu and vv to arrive at the same set.