Restoring trees

4

31 votes
Algorithms, Depth First Search, Easy, Graphs
Problem

You are given the start and finish times of a DFS (Depth-first search) traversal of the vertices that are available in a rooted tree. Your task is to restore the tree.

Input format

  • First line: Contains an integer n that represents the number of vertices of the tree
  • Second line: Contains n space-separated integers that denote the start times of the vertices available in a tree
  • Third line: Contains n space-separated integers that denote the finish times of the vertices available in a tree

Note:

  • In this problem, the start times are 0-based.
  • It is guaranteed that the input data is consistent.

Output format

Print n integers. The ith integer denotes the parent of the ith vertex or 0 if it is the root of the tree.

Note: The vertices of the tree are numbered from 1 to n.

Constraints

n300000

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Note that the interval of a vertex is open-ended ( [startingv,finishingv) ).

Editor Image

?