#P3884. [JLOI2009] 二叉树问题

    ID: 2821 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>2009各省省选吉林深度优先搜索,DFS

[JLOI2009] 二叉树问题

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