#K50292. Employee Hierarchy Path Extraction

    ID: 28832 Type: Default 1000ms 256MiB

Employee Hierarchy Path Extraction

Employee Hierarchy Path Extraction

Given a corporate hierarchy represented as a tree and a target employee, your task is to find the path from the CEO (the root of the tree) to the target employee. The tree is described by a list of employee IDs and a series of direct manager-report pairs. Formally, let ( n ) be the number of employees and ( ids ) be a list of unique employee IDs. A list of pairs ( (u, v) ) signifies that employee ( u ) is the direct manager of employee ( v ). Your program should output the path starting from the CEO (the first ID in the list) and ending with the target, with each ID separated by ' -> '.

( \textbf{Example:} ) If the employee IDs are [1, 2, 3, 4, 5, 6, 7] and the management pairs are [(1,2), (1,3), (2,4), (2,5), (3,6), (3,7)] with target 5, then the output should be "1 -> 2 -> 5".

inputFormat

The input is read from standard input (stdin) and consists of multiple lines as follows:
1. An integer ( n ) representing the number of employees.
2. A line with ( n ) space-separated distinct integers denoting employee IDs.
3. An integer ( m ) representing the number of management pairs.
4. ( m ) lines, each with two space-separated integers ( u ) and ( v ), indicating that employee ( u ) is the manager of employee ( v ).
5. A single integer representing the target employee ID.

outputFormat

Print a single line to standard output (stdout) containing the path from the CEO (the first employee in the list) to the target employee. Each employee ID in the path must be separated by the string " -> ".## sample

7
1 2 3 4 5 6 7
6
1 2
1 3
2 4
2 5
3 6
3 7
5
1 -> 2 -> 5