#C14052. Find All Subordinates

    ID: 43659 Type: Default 1000ms 256MiB

Find All Subordinates

Find All Subordinates

Given an organization's hierarchical structure represented by direct subordinate relationships, your task is to find all direct and indirect subordinates of a given employee.

The organization is provided as a list of relationships. Each relationship consists of a boss and a subordinate. For example, consider the following relationships:

CEO CTO
CEO CFO
CEO COO
CTO Dev Manager
Dev Manager Developer 1
Dev Manager Developer 2
COO HR Manager
HR Manager HR Assistant

For the target employee \(CTO\), the subordinates are:

Dev Manager
Developer 1
Developer 2

If an employee has no subordinates or the target employee does not exist, output nothing.

inputFormat

The input is read from standard input and has the following format:

m
boss1 subordinate1
boss2 subordinate2
... (m lines in total)
target

Where:

  • m is an integer representing the number of direct relationships.
  • Each of the following m lines contains two strings denoting a direct relationship (boss and subordinate). Note that the subordinate name may contain spaces.
  • The last line is the target employee's name.

outputFormat

Print each subordinate (direct or indirect) of the target employee on a new line in the order they are discovered via a depth-first search (DFS). If there is no subordinate, output nothing.

## sample
8
CEO CTO
CEO CFO
CEO COO
CTO Dev Manager
Dev Manager Developer 1
Dev Manager Developer 2
COO HR Manager
HR Manager HR Assistant
CTO
Dev Manager

Developer 1 Developer 2

</p>