#P1232. [NOI2013] 树的计数

    ID: 231 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>递推2013NOI 系列广度优先搜索,BFS深度优先搜索,DFS

[NOI2013] 树的计数

Description

We know that a rooted tree can be traversed by depth-first search (DFS) and breadth-first search (BFS) to generate the DFS order and BFS order of the tree. Two different trees may have the same DFS order, and they may also have the same BFS order. For example, the two trees below both have DFS order 1 2 4 5 3 and BFS order 1 2 3 4 5.

Given a DFS order and a BFS order, we want to know the average height of all rooted trees that satisfy them. That is, suppose there are KK different rooted trees that have this pair of DFS and BFS orders, and their heights are h1,h2,,hKh_1, h_2, \ldots, h_K. Please output:

h1+h2++hKK\frac{h_1+h_2+\ldots+h_K}K

Input Format

The first line contains 11 positive integer nn, the number of nodes of the tree.

The second line contains nn positive integers, a permutation of \, 1n1 \ldots n, representing the DFS order of the tree.

The third line contains nn positive integers, a permutation of \, 1n1 \ldots n, representing the BFS order of the tree.

It is guaranteed that there exists at least one tree consistent with the given two sequences.

Output Format

Output 11 real number, rounded to exactly three decimal places, representing the average tree height.

5 
1 2 4 5 3 
1 2 3 4 5

3.500

Hint

If the absolute difference between your output and the standard answer does not exceed 0.0010.001, you will receive full credit for that test point; otherwise, you will receive no credit.

Constraints

  • For 20%20\% of the testdata: n10n \le 10.
  • For 40%40\% of the testdata: n100n \le 100.
  • For 85%85\% of the testdata: n2×103n \le 2 \times 10^3.
  • For 100%100\% of the testdata: 2n2×1052 \le n \le 2 \times 10^5.

Translated by ChatGPT 5