#C11719. Counting Subordinates in a Company Hierarchy

    ID: 41066 Type: Default 1000ms 256MiB

Counting Subordinates in a Company Hierarchy

Counting Subordinates in a Company Hierarchy

You are given a company hierarchy with n employees numbered from 1 to n. Each employee (except the CEO) has exactly one direct manager. The hierarchy is described by n - 1 pairs of integers, where each pair \((e, m)\) indicates that employee e is directly managed by employee m.

You will also be given q queries. For each query, which is an employee ID, determine the total number of employees who are directly or indirectly managed by that employee. Do not count the manager themselves.

Note: The management relation forms a tree where there is exactly one root (the CEO) with no manager.

The mathematical formulation: For each query v, compute the size of the subtree rooted at v, excluding v itself. That is, if \(T(v)\) denotes the subtree, output \(|T(v)| - 1\).

inputFormat

The input is read from standard input (stdin) and is formatted as follows:

  • The first line contains two integers n and q, where n is the number of employees and q is the number of queries.
  • The next n - 1 lines each contain two integers e and m, representing that employee e is directly managed by employee m.
  • The last line contains q space-separated integers, each representing an employee ID for which you must compute the number of subordinates.

outputFormat

Print a single line to standard output (stdout) containing q integers. Each integer corresponds to the number of employees (both directly and indirectly) managed by the corresponding queried employee. The answers should be output in the same order as the queries, separated by spaces.

## sample
1 1
1
0

</p>