C - 会社の役職と給与 / Company Positions and Salaries Editorial by admin
gemini-3.5-flash-thinkingOverview
This problem involves employees with a tree-structured hierarchy, where we must maximize the total salary rank while satisfying several constraints (the president’s rank is \(1\), a subordinate’s rank must be at least their supervisor’s rank, and each employee’s rank must not exceed their upper limit).
Analysis
To maximize the total salary rank, we want to make each employee’s salary rank as large as possible.
Between a direct supervisor \(P_i\) and subordinate \(i\), there is a relationship on salary rank \(R\): \(R_{P_i} \leq R_i\). From a different perspective, this can be rephrased as “a supervisor’s salary rank must be less than or equal to their subordinate’s salary rank.”
Since subordinate \(i\)’s salary rank has an upper limit of \(U_i\), supervisor \(P_i\)’s salary rank \(R_{P_i}\) is also restricted to be at most \(U_i\). Applying this relationship recursively, we can see that an employee \(u\)’s salary rank must be at most the minimum upper limit among all subordinates of \(u\) (all employees in the subtree rooted at \(u\), including children, grandchildren, etc.).
Therefore, we define the “effective upper limit” \(U'_i\) for each employee \(i\) as the minimum value of \(U\) in the subtree rooted at \(i\).
To maximize the total salary rank, for employees other than the president (\(i \geq 2\)), it is optimal to set the salary rank equal to this effective upper limit \(U'_i\). Since the president (employee 1)’s salary rank is fixed at \(1\), we have the following two cases:
- If the president’s effective upper limit \(U'_1\) is less than \(1\) (i.e., \(0\)) The president’s salary rank cannot be set to \(1\), so no valid assignment exists, and the answer is \(-1\).
- Otherwise By setting the president’s salary rank to \(1\) and each other employee \(i\)’s salary rank to \(U'_i\), we can maximize the total while satisfying all constraints. (By the definition of the subtree minimum, since supervisor \(P_i\)’s subtree contains subordinate \(i\)’s subtree, \(U'_{P_i} \leq U'_i\) always holds, and the constraints are satisfied.)
Algorithm
We need to efficiently compute the effective upper limit \(U'_i\) for each employee.
Due to the constraint \(P_i < i\), processing employees in decreasing order of their number naturally traverses the tree in a “leaf-to-root (bottom-up)” order.
- Loop over employees \(i\) from \(N\) down to \(2\) in reverse order.
- Using employee \(i\)’s upper limit \(U_i\), update the direct supervisor \(P_i\)’s upper limit as \(U_{P_i} = \min(U_{P_i}, U_i)\).
- After the loop ends, if \(U_1 < 1\), output
-1. - Otherwise, output the sum of the president’s salary rank \(1\) and the updated upper limits \(U_i\) for employees \(2\) through \(N\).
Complexity
- Time complexity: \(O(N)\) The bottom-up transitions via the reverse loop take constant time per vertex, resulting in \(O(N)\) overall, which is very fast.
- Space complexity: \(O(N)\) Proportional to the array sizes needed to store the supervisor information \(P\) and upper limit ranks \(U\) for employees.
Implementation Notes
Since the property \(P_i < i\) (similar to a topological sort order) is guaranteed, there is no need to implement DFS (depth-first search) with recursion or tree DP. A simple reverse for loop (range(N, 1, -1)) is sufficient to achieve the bottom-up transitions concisely and efficiently.
Source Code
import sys
def solve():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
U = [0] * (N + 1)
U[1] = int(input_data[1])
P = [0] * (N + 1)
idx = 2
for i in range(2, N + 1):
P[i] = int(input_data[idx])
U[i] = int(input_data[idx+1])
idx += 2
for i in range(N, 1, -1):
p = P[i]
if U[i] < U[p]:
U[p] = U[i]
if U[1] < 1:
print(-1)
else:
ans = 1 + sum(U[2:])
print(ans)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3.5-flash-thinking.