'CS50's Tideman: lock_pairs fails to skip the final pair if it creates a cycle (supposedly)

I'm stuck at the lock_pairs function of Tideman. I already wrote the code for it and it looks good. It also behaves as it should when running it through the debugger; however, every time I try it with check50 it gives me an error on "lock pair skips final pair if it creates a cycle". In order to try it out, I've used examples where the final pair is the one which should be skipped and everything goes as planned when I watch the whole process step by step on the debugger, so I really don't know what I'm missing here.

Here's the explanation of the problem: https://cs50.harvard.edu/x/2022/psets/3/tideman/

void lock_pairs(void)
{
    // TODO
    // When no_cycle is 0, it indicates that there is no cycle and that locking the pair is safe
    int no_cycle = 0;

    for (int i = 0; i < pair_count; i++)
    {
        for (int j = 0; j < pair_count; j++)
        {
            // We check each pair, looking for a locked pair where the loser of the pair we're checking is the winner.
            if (!locked[pairs[i].loser][pairs[j].loser])
            {
                no_cycle = 0;
                continue;
            }
            else
            {
                // If the result is true, we check for a possible cycle using the cycle_check function
                if (cycle_check(i, j) == false)
                {
                    // If the result is false, we lock the pair and move on
                    no_cycle = 0;
                    break;
                }
                else
                {
                    // If the result is true (indicating a cycle), we change the value of no_cycle to avoid locking the pair
                    no_cycle = 1;
                    break;
                }
            }
        }
        if (no_cycle == 0)
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
        }
        else continue;
    }
    return;
}

bool cycle_check(i, j)
{
    // We check if the loser of the locked pair is the same as our pair's winner or starting point (the end of the chain is also the begining)
    if (pairs[j].loser == pairs[i].winner)
    {
        return true;
    }
    else
    {
        //If it isn't, we go through each locked pair, following the chain which could lead to the cycle
        for (int h = 0; h < pair_count; h++)
        {
            if (locked[pairs[j].loser][pairs[h].loser])
            {
                // Once we find the next link in the chain, we start over
                j = h;
                if (cycle_check(i, j) == true)
                {
                    // Once we find that there is indeed a cycle, we break recursion
                    return true;
                }
            }
        }
        // If we follow the chain but don't find a cycle, the function returns false and the pair is locked
        return false;
    }
}

Been stuck forever on this. Any help would be much appreciated.

Btw, english is not my primary language, so please excuse any grammar errors.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source