Tree Count
Time limit1sMemory limit512 MB
Given a DFS ordering and a BFS ordering of a rooted tree's vertices, find the average height over all rooted trees that produce both orderings.
- Level
Hard8 of 10
- Topics
- Tree, DFS, BFS, Dynamic programming
- Solved
- No attempts yet
Problem
A rooted tree can be traversed with depth-first search (DFS) and breadth-first search (BFS) to obtain the DFS and BFS orderings of its vertices. Two different trees can have the same DFS ordering, and their BFS orderings can also be the same. For example, the two trees below both have DFS ordering 1 2 4 5 3 and BFS ordering 1 2 3 4 5. 
Given a DFS ordering and a BFS ordering, find the average height of all rooted trees that have both orderings. If different rooted trees have both orderings and their heights are , output .
Input
The first line contains a single integer , the number of vertices. The second line contains integers from 1 to , the DFS ordering. The third line contains integers from 1 to , the BFS ordering. At least one tree has both orderings.
Output
Print one real number, the average height of the trees. An answer is accepted if its absolute error is at most 0.001. A tree with one vertex has height 1. Otherwise, its height is 1 plus the maximum height among the subtrees rooted at its children.