Think of the locked array as a directed graph. Each candidate is a node. When you lock a pair (winner → loser), you draw an arrow from the winner to the loser.
Here is a potential implementation in Python: Cs50 Tideman Solution
If a candidate appears earlier in ranks than another, the voter prefers them. Increment preferences[ranks[i]][ranks[j]] where i < j . 3. Add Pairs Think of the locked array as a directed graph
return -1;
Identify all winning pairs where one candidate beats another. Here is a potential implementation in Python: If
The lock_pairs function builds a directed acyclic graph (DAG) inside the 2D boolean array locked[i][j] . This is where most students get stuck.
The winner is the candidate with (i.e., no locked[j][i] true for any j ). Loop through each candidate, and if none of the other candidates have a locked edge pointing to them, that candidate wins.