luogu#P5002. 专心OI - 找祖先
专心OI - 找祖先
Background
Imakf is a beginner. He has recently learned LCA. He saw a game in the mobile App Store that is also called LCA, so he downloaded it.
Problem Description
This game gives you a tree with nodes, where the root is . The system will select nodes , and asks Imakf to answer how many ordered pairs of nodes have their lowest common ancestor equal to . Imakf is a beginner. Even though he has learned LCA, he still cannot solve it, so he has to ask you for help.
Input Format
The first line contains three integers .
Then lines follow, each containing two numbers , indicating that there is an edge between and .
Then line follows, containing numbers, indicating .
It is guaranteed that the given edges form a tree.
Output Format
Output lines in total. Each line contains one number. The number on line indicates how many ordered pairs of nodes have their lowest common ancestor equal to .
7 1 3
1 2
1 3
2 4
2 5
3 6
3 7
1 2 4
31
7
1
Hint
The tree of Sample 1 is shown in the figure below:

For Query 1 $~(1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (2,1) (2,3) (2,6) (2,7) (3,1) (3,2) (3,4) (3,5) (4,1) (4,3)$
$(4,6) (4,7) (5,1) (5,3) (5,6) (5,7) (6,1) (6,2) (6,4) (6,5) (7,1) (7,2) (7,4) (7,5)$ there are ordered pairs in total.
For Query 2 there are ordered pairs in total.
For Query 3 there is ordered pair in total.
Constraints: , .
Translated by ChatGPT 5