Restoring trees
Practice
4 (31 votes)
Algorithms
Depth first search
Easy
Graphs
Problem
88% Success 4405 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
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 \(i^{th}\) integer denotes the parent of the \(i^{th}\) vertex or \(0\) if it is the root of the tree.
Note: The vertices of the tree are numbered from 1 to \(n\).
Constraints
\(n \le 300000\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
30 votes
Tags:
Depth First SearchEasyGraphs
Points:30
19 votes
Tags:
AlgorithmsDepth First SearchEasyGraphs
Points:30
7 votes
Tags:
AlgorithmsBinary SearchDepth First SearchEasySearching
Editorial