#P14260. 期待(counting)

    ID: 13454 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2025洛谷原创O2优化深度优先搜索 DFS前缀和洛谷月赛

期待(counting)

Description

Little Y and Little S have a tree with nn nodes. Each of them has a game piece. Initially, they arbitrarily choose two points u0,v0u_0, v_0: Little Y places his piece at u0u_0, and Little S places her piece at v0v_0.

Subsequently, they move their pieces simultaneously. Each time, they move their own piece along an edge of the tree to an adjacent node. However, if this is not the first move, they cannot move the piece back along the edge used in the previous step.

They can move the pieces any number of times. Let the positions of the two pieces after the ii-th move be ui,viu_i, v_i, respectively. They call a movement scheme counted if for every ii, the distance between uiu_i and viv_i is equal to the distance between u0u_0 and v0v_0.

The distance between two points uu and vv is defined as the number of edges in the unique simple path between them on the tree. In particular, if the two points are the same, the distance is 00.

Now, they have chosen two distinct points yy and ss. They want to know how many different counted movement schemes there are such that Little Y's piece passes through yy, and Little S's piece passes through ss. The two pieces do not need to pass through these points simultaneously. Formally, there exist i,ji, j such that ui=yu_i = y and vj=sv_j = s, and it is not required that i=ji = j.

Two movement schemes are considered different if and only if the number of moves in the two schemes is different, or for some ii, at least one of uiu_i or viv_i differs between the two schemes.

Input Format

This problem contains multiple test cases.

The first line of input contains a positive integer TT, representing the number of test cases.

This is followed by TT test cases, each formatted as follows:

The first line contains three integers n,y,sn, y, s, indicating the number of nodes in the tree and the two given points, respectively. It is guaranteed that ysy \neq s.

The next n1n-1 lines:

The ii-th line contains two integers ai,bia_i, b_i, indicating that the ii-th edge connects nodes aia_i and bib_i.

Output Format

For each test case, output a single integer in a line indicating the answer.

2
4 1 3
1 2
1 3
1 4
4 2 3
1 2
1 3
1 4

13
5

Hint

【Sample 1 Explanation】

The structure of the tree is shown in the figure below.

Let (u,v)(u, v) denote that Little Y's piece is at uu and Little S's piece is at vv.

For the first test case, the following counted movement schemes cause Little Y's piece to pass through 11 and Little S's piece to pass through 33:

::cute-table{tuack}

Scheme ID Movement Sequence Scheme ID Movement Sequence
11 (2,1)(1,3)(2,1)\to(1,3) 22 (1,3)(2,1)(1,3)\to(2,1)
33 (4,1)(1,3)(4,1)\to(1,3) 44 (1,3)(4,1)(1,3)\to(4,1)
55 (1,1)(3,3)(1,1)\to(3,3) 66 (3,3)(1,1)(3,3)\to(1,1)
77 (2,2)(1,1)(3,3)(2,2)\to(1,1)\to(3,3) 88 (3,3)(1,1)(2,2)(3,3)\to(1,1)\to(2,2)
99 (4,4)(1,1)(3,3)(4,4)\to(1,1)\to(3,3) 1010 (3,3)(1,1)(4,4)(3,3)\to(1,1)\to(4,4)
1111 (1,3)(3,1)(1,3)\to(3,1) 1212 (3,1)(1,3)(3,1)\to(1,3)
1313 (1,3)(1,3)

Total: 1313 schemes.

For the second test case, the following counted movement schemes cause Little Y's piece to pass through 22 and Little S's piece to pass through 33:

::cute-table{tuack}

Scheme ID Movement Sequence Scheme ID Movement Sequence
11 (2,2)(1,1)(3,3)(2,2)\to(1,1)\to(3,3) 22 (3,3)(1,1)(2,2)(3,3)\to(1,1)\to(2,2)
33 (2,1)(1,3)(2,1)\to(1,3) 44 (1,3)(2,1)(1,3)\to(2,1)
55 (2,3)(2,3)

Total: 55 schemes.

【Sample 2】

See counting2.in and counting2.ans in the problem attachment.

This sample satisfies the special properties of test data 1.

【Sample 3】

See counting3.in and counting3.ans in the problem attachment.

This sample satisfies special property A.

【Sample 4】

See counting4.in and counting4.ans in the problem attachment.

This sample satisfies special property B, where the first two test cases satisfy n103n \le 10^3.

【Sample 5】

See counting5.in and counting5.ans in the problem attachment.

This sample satisfies special property C, where the first two test cases satisfy n103n \le 10^3.

【Data Range】

For all test data, it is guaranteed:

  • 1T51 \le T \le 5;
  • 1y,s,ai,bin1051 \le y, s, a_i, b_i \le n \le 10^5;
  • ysy \neq s.

::cute-table{tuack}

Test Data ID nn \le Special Property Test Data ID nn \le Special Property
131\sim3 100100 None 111311\sim13 10510^5 A
454\sim5 10310^3 B 141514\sim15 ^ B
676\sim7 ^ C 161716\sim17 C
8108\sim10 None 182018\sim20 None

Special Property A: The tree is a complete binary tree, i.e., for i2i \ge 2, node ii is connected to node i2\lfloor \frac{i}{2} \rfloor.

Special Property B: The tree is a star graph, i.e., there exists one node connected to all other nodes.

Special Property C: The tree is a path, i.e., the degree of every node does not exceed 22.