Maximum Employees to Be Invited to a Meeting⚓︎
Description⚓︎
A company is organizing a meeting and has a list of n
employees, waiting to be invited. They have arranged for a large circular table, capable of seating any number of employees.
The employees are numbered from 0
to n - 1
. Each employee has a favorite person and they will attend the meeting only if they can sit next to their favorite person at the table. The favorite person of an employee is not themself.
Given a 0-indexed integer array favorite
, where favorite[i]
denotes the favorite person of the i
-th employee, return the maximum number of employees that can be invited to the meeting.
Example 1:
- Input:
favorite = [2,2,1,2]
- Output:
3
- Explanation:
Example 2:
- Input:
favorite = [1,2,0]
- Output:
3
- Explanation:
Example 3:
- Input:
favorite = [3,0,1,4,1]
- Output:
4
- Explanation:
Constraints:
n == favorite.length
2 <= n <= 10^5
0 <= favorite[i] <= n - 1
favorite[i] != i
Solution⚓︎
See reference (Chinese).
Pseudotrees:
A pseudotree is a connected graph that slightly deviates from the strict definition of a tree. While a tree is a connected, acyclic graph with \(n\) vertices and \(n-1\) edges, a pseudotree has \(n\) vertices and \(n\) edges, introducing exactly one cycle into the structure. This singular cycle is what distinguishes a pseudotree from a tree. Despite the presence of this cycle, the graph remains minimally connected, meaning if any edge is removed, the graph will no longer be connected.
Characteristics:
- Singular Cycle: The defining characteristic of a pseudotree is the presence of exactly one cycle. This means there's precisely one path where you can start and end at the same vertex without traversing any edge more than once.
- Connectivity: A pseudotree remains connected, ensuring there's a path between any two vertices.
- Edge Count: With \(n\) vertices, a pseudotree has \(n\) edges, which is one more than a tree with the same number of vertices.
Pseudoforests:
A pseudoforest extends the concept of pseudotrees to a collection of disconnected graphs, where each component graph is a pseudotree. Therefore, a pseudoforest with \(k\) pseudotrees has \(n\) vertices and \(n+k\) edges, where each component pseudotree contains exactly one cycle.
Characteristics:
- Disjoint Pseudotrees: A pseudoforest is made up of multiple pseudotrees that are not connected to each other.
- Cycle Limit: Each component pseudotree in a pseudoforest contains exactly one cycle.
Directed Pseudotrees:
In directed graphs, pseudotrees can be categorized into in-trees and out-trees based on the direction of edges:
- In-Tree: Every vertex except for one (which lies on the cycle) has an in-degree of 1, meaning all edges direct towards the cycle or a path leading to the cycle.
- Out-Tree: Conversely, every vertex except for one has an out-degree of 1, indicating that all edges emanate from the cycle or lead away from it.
Code:
Approach:
- Initialization and In-Degree Calculation:
- The
inDegree
vector is used to store the number of people who have each employee as their favorite. This is a crucial step to identify employees with no one favoring them, indicating the start points for a topological sort. -
queue<int> que
is initialized to store employees in the order they are to be processed in topological sorting. -
Topological Sorting to Find 'In-Trees':
- The algorithm employs a queue to perform a topological sort based on in-degrees. Employees with an in-degree of 0 (no one's favorite) are added to the queue as starting points.
- In the context of pseudoforests, an employee with in-degree 0 can be seen as an isolated node or the end node of an "in-tree" where paths (indicating favoritism) lead inwards towards a cycle or another tree.
-
As employees are processed (removed from the queue), their favorite person's in-degree is decremented. If this decrement results in an in-degree of 0, that employee is then added to the queue. This step effectively performs a layer-by-layer traversal of the in-trees, calculating the maximum depth from the outermost layer towards the cycle or root.
-
Calculating Maximum Depth and Identifying Cycles:
maxDepth
vector stores the maximum distance of each employee from the end of their in-tree, aiding in later calculations for employees involved in special 2-person cycles.-
After the topological sort, the remaining employees with non-zero in-degrees are part of cycles. The algorithm then iterates through each employee to find these cycles and classify them.
-
Handling Special 2-Person Cycles:
- A special case in this problem is a 2-person cycle, where each person is the favorite of the other. Such pairs can have additional people attached to them (forming chains that lead into this pair) but cannot be part of a larger cycle themselves.
-
For each 2-person cycle found, the algorithm calculates the sum of the maximum depths of the chains leading into each member of the pair (since both can invite their chains of admirers) and adds 2 (for the pair itself) to
sumOfSpecialCircles
. -
Handling Regular Cycles:
- For cycles larger than 2 persons, only the members of the cycle can be invited since inviting anyone outside the cycle (except in the special 2-person case) would break the condition of everyone sitting next to their favorite.
-
The algorithm keeps track of the largest such cycle with
regularCircle
. -
Result Calculation:
-
The maximum number of invitations is the larger of
sumOfSpecialCircles
andregularCircle
. This is because the meeting can either optimize for a series of connected 2-person cycles with their chains or a single large cycle, whichever is bigger. -
Time complexity: \(O(n)\);
- Space complexity: \(O(n)\).