TopCoder problem "CorporationSalary" used in SRM 407 (Division I Level One , Division II Level Two)

Problem Statement

    You are working in the HR department of a huge corporation. Each employee may have several direct managers and/or several direct subordinates. Of course, his subordinates may also have their own subordinates, and his direct managers may have their own managers. We say employee X is a boss of employee Y if there exists a sequence of employees A, B, ..., D, such that X is the manager of A, A is the manager of B, and so on, and D is the manager of Y (of course, if X is a direct manager of employee Y, X will be a boss of employee Y). If A is a boss of B, then B can not be a boss of A. According to the new company policy, the salary of an employee with no subordinates is 1. If an employee has any subordinates, then his salary is equal to the sum of the salaries of his direct subordinates.

You will be given a String[] relations, where the j-th character of the i-th element is 'Y' if employee i is a direct manager of employee j, and 'N' otherwise. Return the sum of the salaries of all the employees.


Method signature:long totalSalary(String[] relations)
(be sure your method is public)


-relations will contain between 1 and 50 elements, inclusive.

-Each element of relations will contain the same number of characters, which is equal to number of elements in relations.

-Each element of relations will contain only 'Y' or 'N'.

-Character i of element i of relations will be 'N' for each i.

-If A is a boss of B, then B will not be a boss of A.


Returns: 1
Here we've got only one employee so the answer is 1.
Returns: 5
There are 4 employees here. 0, 1 and 3 are managers of 2, and also 3 is a manager of 1. So:

salary(2) = 1

salary(0) = salary(2) = 1

salary(1) = salary(2) = 1

salary(3) = salary(2) + salary(1) = 2
Returns: 17
Returns: 8
Returns: 4

Problem url:

Problem stats url:




PabloGilberto , Olexiy , ivan_metelsky , ged

Problem categories:

Graph Theory