#P3899. [湖南集训] 更为厉害
[湖南集训] 更为厉害
Description
Let be a rooted tree. We make the following definitions:
- Let and be two distinct nodes in . If is an ancestor of , then we say " is more formidable than ."
- Let and be two distinct nodes in . If the distance between and in the tree is no more than a given constant , then we say " and are about the same."
Given a rooted tree with nodes, numbered from to , where the root is node . You need to answer queries. In each query, two integers and are given. Determine how many ordered triples satisfy:
- are three distinct nodes in , and is node .
- and are both more formidable than .
- and are about the same, where the constant in “about the same” is the given .
Input Format
The first line contains two positive integers and , representing the number of nodes in the rooted tree and the number of queries, respectively.
The next lines each describe an edge of the tree. Each line contains two integers and , indicating that there is an edge between nodes and .
The next lines each describe an operation. The -th line contains two integers, representing and for the -th query.
Output Format
Output lines. Each line corresponds to a query and contains the answer to that query.
5 3
1 2
1 3
2 4
4 5
2 2
4 1
2 3
3
1
3
Hint
The tree in the sample is shown below:

For the first and third queries, the valid triples are , , and .
For the second query, the only valid triple is .
The constraints for all test points are as follows (note that Luogu does not evaluate according to the following):

For all queries in the full testdata, .
- On 2023.9.15, a set of hack testdata was added.
Translated by ChatGPT 5
京公网安备 11011102002149号