#P4674. Minimum Salary Restructuring
Minimum Salary Restructuring
Minimum Salary Restructuring
A company of n employees is undergoing a restructuring. In the resulting hierarchy, represented as a rooted tree, every node becomes the boss of its children.
Each employee has a list of bosses they will accept as their immediate superior, and each employee must be assigned a positive integer salary. The salary assignment must satisfy the condition that the salary of every boss is strictly larger than the sum of the salaries of their immediate subordinates. In the minimal assignment, each employee's salary is set exactly to one more than the sum of the salaries of their children. It can be shown that, in this case, the total sum of salaries equals \[ n+\sum_{i=1}^{n}{\rm depth}(i), \] where \({\rm depth}(i)\) is the distance from the root in the tree.
Your task is to choose a root and assign each employee (except the root) a boss from their acceptable list so that all employees are connected in a tree and the sum of the depths is minimized. Equivalently, you are to minimize the total salary cost. If no valid hierarchy exists covering all employees, output -1
.
inputFormat
The input begins with an integer \(n\) (\(1 \le n \le 10^5\)), representing the number of employees.
Then follow \(n\) lines. The \(i\)th line describes the acceptable bosses for employee \(i\). It starts with an integer \(m_i\) (\(0 \le m_i \le n-1\)), which is the number of acceptable bosses, followed by \(m_i\) distinct integers denoting the IDs of these bosses (each between \(1\) and \(n\)).
Note: If an employee is chosen as the root, their list is ignored.
outputFormat
Output a single integer representing the minimum total sum of salaries in the company under an optimal restructuring. Recall that if the minimal salary assignment is used, the total sum of salaries equals \(n + \sum_{i=1}^{n}{\rm depth}(i)\). If it is impossible to form a valid hierarchy (i.e. a spanning tree) meeting the acceptable boss criteria, output -1
.
sample
3
0
1 1
1 1
5