Players Whose Leaderboard Position Is Forced
Trees & GraphsAuto-graded · 20 test casesLast updated
Find every player comparable to all others in a consistent win/loss DAG, using transitive reachability in both directions, ideally with bitsets.
A tournament has n players, numbered 1 through n. Only some games have been played. Each result is a pair [winner, loser] in the list results, meaning winner finished strictly above loser.
Rankings are transitive. If a is above b and b is above c, then a is above c. Those are the only inferences you may make. Two players you cannot compare through a chain of results may be in either order in some valid final ordering.
A player's final position is forced when every valid complete ordering consistent with the results puts that player in the same place. This happens exactly when, for every other player x, you can infer that the player is above x or that x is above the player (through a direct result or a chain of results).
The results are guaranteed to be consistent, so they contain no cycles.
Return the labels of all players whose position is forced, as a list sorted in ascending order.
Special cases:
- If
n = 1, the only player is trivially forced, so the answer is[1]. - If there are no results and
n > 1, nobody is forced, so the answer is[]. - A player with no connection to the others cannot be forced when
n > 1.
Constraints
1 <= n <= 5000 <= len(results) <= 10^4- Each entry is
[winner, loser]with1 <= winner, loser <= nandwinner != loser. - All pairs in
resultsare distinct. - The results contain no cyclic contradictions.
- Return the player labels in increasing order.
Examples
Example 1
Input: forced_position_players(5, [[4, 3], [4, 2], [3, 2], [1, 2], [2, 5]])
Output: [2, 5]
Example 2
Input: forced_position_players(3, [[1, 2], [2, 3]])
Output: [1, 2, 3]
Hints
Hint 1
Treat each result as a directed edge winner -> loser. A player is known to be above another exactly when a directed path leads from one to the other.
Hint 2
A player's position is forced exactly when the number of players it can reach plus the number of players that can reach it equals n - 1. In a DAG these two groups never overlap.
Hint 3
Compute reachability with one bitmask per player, processing nodes in topological order. Do a forward pass for 'who can reach me' and a reverse pass for 'who I can reach', so you avoid an O(n^3) closure.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Build a directed graph with an edge winner -> loser. Because ranking is transitive, one player is known to be above another exactly when there is a directed path between them. No other facts can be deduced: if neither player can reach the other, some valid ordering puts the first higher and another puts the second higher.
A player p has a forced position exactly when every other player is either reachable from p (below p) or can reach p (above p). In that case exactly |above(p)| players sit higher in every valid ordering, so the position is fixed. If some player x is incomparable with p, there are valid orderings with x above p and with x below p, which shifts p's position, so it is not forced.
To compute the two sets efficiently:
- Get a topological order with Kahn's algorithm.
- Store each set as a Python integer bitmask. Walking the order in reverse,
below[u]is the OR over childrenvof(1 << v) | below[v]. Every child is finished before its parent is read. - Walking the order forward, push
(1 << u) | above[u]into every child'sabovemask. This gives the ancestor sets directly, with no per-bit transpose. - Player
pqualifies whenpopcount(below[p] | above[p]) == n - 1. The graph is acyclic, sopis in neither set, and the two sets are disjoint.
Iterating p from 0 to n - 1 and appending p + 1 yields a sorted result.
The edge cases need no special handling. With n = 1 both masks are 0, which equals n - 1 = 0, so the answer is [1]. With no results and n > 1, every popcount is 0, so the answer is []. Redundant edges are harmless because OR is idempotent.
Complexity
- Time:
O(n + m)for the topological sort, plusO(m * n / w)for the two bitmask passes, wherewis the machine word size. The final popcount check costsO(n * n / w). - Space:
O(n^2 / w)for the masks, plusO(n + m)for the adjacency lists.
Reference solution
from typing import List
def forced_position_players(n: int, results: List[List[int]]) -> List[int]:
children = [[] for _ in range(n)]
indeg = [0] * n
for w, l in results:
w -= 1
l -= 1
children[w].append(l)
indeg[l] += 1
# Topological order (Kahn)
order = [i for i in range(n) if indeg[i] == 0]
idx = 0
while idx < len(order):
u = order[idx]
idx += 1
for v in children[u]:
indeg[v] -= 1
if indeg[v] == 0:
order.append(v)
# below[u]: bitmask of players that u is known to be above
below = [0] * n
for u in reversed(order):
m = 0
for v in children[u]:
m |= (1 << v) | below[v]
below[u] = m
# above[v]: bitmask of players known to be above v
above = [0] * n
for u in order:
carry = (1 << u) | above[u]
for v in children[u]:
above[v] |= carry
answer = []
for p in range(n):
known = below[p] | above[p]
if bin(known).count("1") == n - 1:
answer.append(p + 1)
return answer