#LCA0000042. Loại một nhân viên để tăng độ sâu tổ tiên (Company)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Loại một nhân viên để tăng độ sâu tổ tiên (Company)
Company
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A company has employees indexed through , with employee as director. Their reporting structure is a tree rooted at . The root depth is ; every other depth is its parent's depth plus .
Each plan involves exactly the employees with indices in , where . You must remove exactly one employee from the plan. The most deeply nested manager who can manage all remaining employees is their lowest common ancestor. Choose to maximize the depth of this manager and print the removed employee index and the resulting optimal depth.
If several removals give the same optimum, any one is accepted. A special checker validates all optimal alternatives.
Input
- First line: .
- Next line: parents .
- Last lines: ranges with .
Output
For each query print , the removed employee index and the maximum achievable manager depth. Any optimal is accepted.
Subtasks
General constraints (all subtasks): ; ; ; .
- Subtask 1 (20 points): ; ; ; a parent index can exceed its child index.
- Subtask 2 (30 points): ; ; ; a parent index can exceed its child index.
- Subtask 3 (50 points): ; ; ; a parent index can exceed its child index.
Examples
Input
5 3
1 1 3 3
4 5
2 5
1 3
Output
4 2
2 1
1 0
Explanation
For , removing leaves vertex at depth ; removing also gives depth . The sample prints one valid optimal choice. The other queries are handled by the LCA of the remaining employee set.