QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 256 MB Puntuación total: 100 Hackeable ✓

#6778. Counting Trees

Estadísticas

We know that a rooted tree can be traversed using Depth-First Search (DFS) and Breadth-First Search (BFS) to generate its DFS order and BFS order, respectively. Two different trees may have the same DFS order, and they may also have the same BFS order. For example, the two trees shown below both have the DFS order 1 2 4 5 3 and the BFS order 1 2 3 4 5.

Given a DFS order and a BFS order, we want to find the average height of all rooted trees that satisfy these conditions. That is, if there are $K$ different rooted trees that have this specific DFS order and BFS order, and their heights are $h_1, h_2, \dots, h_K$, please output:

\begin{equation} \frac{h_1 + h_2 + \dots + h_K}{K} \end{equation}

Input

The first line contains one positive integer $n$, representing the number of nodes in the tree.

The second line contains $n$ positive integers, which is a permutation of $1 \sim n$, representing the DFS order of the tree.

The third line contains $n$ positive integers, which is a permutation of $1 \sim n$, representing the BFS order of the tree.

It is guaranteed that at least one tree exists that matches the given two sequences.

Output

Output a single real number, rounded to exactly three decimal places, representing the average height of the trees.

Examples

Input 1

5 
1 2 4 5 3 
1 2 3 4 5

Output 1

3.500

Constraints

If the difference between your output and the standard output is no more than $10^{-3}$, you will receive points for that test case; otherwise, you will receive no points.

$20\%$ of the test data satisfies: $n \leq 10$;

$40\%$ of the test data satisfies: $n \leq 100$;

$85\%$ of the test data satisfies: $n \leq 2000$;

$100\%$ of the test data satisfies: $2 \leq n \leq 200000$.

Note

Tree height: If a rooted tree contains only one root node, its height is $1$. Otherwise, its height is the maximum height of all subtrees of the root node plus $1$.

For any three nodes $a, b, c$ in the tree, if $a$ and $b$ are both children of $c$, then the relative order of $a$ and $b$ in the BFS order and the DFS order is consistent; that is, either $a$ is before $b$ in both, or $a$ is after $b$ in both.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.