Hints for SRM 679, D1, 250-Pointer (FiringEmployees)
James S. Plank
Sat Feb 13 11:56:38 EST 2016
Problem Statement.
This is a straightforward postorder tree traversal.
You need to turn the input into a tree where each node can have any number of
children. You do that by having each node maintain a vector of its children.
Then do a postorder traversal. For each node, you want to
calculate the node's profit after potentially firing employees in subtrees, or even
firing the employee corresponding to the node. The node's profit is going to be the
maximum of:
- Zero -- this is what happens if you fire the manager corresponding to the node.
- The manager's profit + Postorder(each employee that reports directly to the manager).
How about an example -- the example below is similar to
example 3 in the Topcoder writeup, but I've changed the productivities to make
it more interesting:
Employee |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
manager |
0 |
1 |
2 |
1 |
2 |
3 |
4 |
2 |
3 |
salary |
5 |
3 |
6 |
8 |
4 |
2 |
4 |
6 |
7 |
productivity |
2 |
1 |
3 |
6 |
6 |
3 |
5 |
10 |
8 |
From that, you can build a tree which holds the profit of each employee, plus
a link to the employees that the employee manages. Note that you have to include the CEO as employee zero:
Now, here's the result of the postorder traversal. I've colored the
employees by what has happened to them, although you don't have to
make any note of this when you program -- you just need to keep track
of their profit. The answer here will be one.