National Olympiad in Informatics, China, 2013
We know that a rooted tree can be traversed via depth-first search (DFS)
and breadth-first search (BFS) to obtain the DFS and BFS orderings of
their vertices. Two different trees can have the same DFS ordering, and
at the same time their BFS orderings can also be the same. For example,
the following two trees both have a DFS ordering of 1 2 4 5 3
and a BFS
ordering of 1 2 3 4 5
.
Given a DFS and BFS ordering, we would like to know the average height
of all rooted trees satisfying the condition. For example, if there are
Input Specification
The first line contains a single positive integer
The second line contains
The third line contains
The input guarantees that at least one tree satisfying the two orderings
will exist.
Output Specification
Output a single real number, rounded half-up to three places after the decimal point, representing the average height of the trees.
Sample Input
5
1 2 4 5 3
1 2 3 4 5
Sample Output
3.500
Grading
If your output differs from the correct answer by no more than
Constraints
Hints
If a rooted tree has only one vertex, then its height is
For any three vertices
Problem translated to English by .
Comments