luogu#P3884. [JLOI2009] 二叉树问题

    ID: 7912 Type: RemoteJudge 1000ms 128MiB Tried: 23 Accepted: 7 Difficulty: 5 Uploaded By: Tags>搜索最近公共祖先LCA树上距离树链剖分树剖深度优先搜索DFS各省省选2009吉林

[JLOI2009] 二叉树问题

Problem Description

For the binary tree shown below, its depth, width, and distances between nodes are as follows:

  • Depth: 44.
  • Width: 44.
  • The distance between nodes 8 and 6: 88.
  • The distance between nodes 7 and 6: 33.

Here, the width is the maximum number of nodes on the same level of the binary tree. The distance between nodes uu and vv is defined as twice the number of edges directed toward the root plus the number of edges directed toward the leaves, along the shortest directed path from uu to vv.

Given a binary tree rooted at node 1, compute its depth, width, and the distance between two specified nodes x,yx, y.

Input Format

The first line contains an integer nn, the number of nodes in the tree.
The next n1n - 1 lines each contain two integers u,vu, v, indicating that there is an edge connecting uu and vv in the tree.
The last line contains two integers x,yx, y, asking for the distance between xx and yy.

Output Format

Output three lines, each containing one integer, representing the depth, the width, and the distance between xx and yy, in this order.

10                                
1 2                            
1 3                            
2 4
2 5
3 6
3 7
5 8
5 9
6 10
8 6

4
4
8

Hint

For all test points, 1u,v,x,yn1001 \leq u, v, x, y \leq n \leq 100, and the input describes a tree. It is guaranteed that uu is the parent of vv.

Translated by ChatGPT 5