Buggy python submission (verdict: Time Limit Exceeded):
def list_up_next_games(players):
    games = set()
    for p in players:
        if A[p]:
            opponent = A[p][-1] - 1
            if A[opponent][-1] - 1 == p:
                if p < opponent:
                    games.add((p, opponent))
                else:
                    games.add((opponent, p))
    return(set(games))


N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    A[i].reverse()

remain_games = []
for i in range(N):
    for j in range(i+1,N):
        remain_games.append((i,j))

games = list_up_next_games(list(range(N)))

days = 0
while games:
    days += 1
    players = []
    for game in games:
        remain_games.remove(game)
        A[game[0]].pop()
        A[game[1]].pop()
        players.append(game[0])
        players.append(game[1])
    games = list_up_next_games(players)
if remain_games:
    print(-1)
else:
    print(days)

Fixed submission:
def list_up_next_games(players):
    games = set()
    for p in players:
        if A[p]:
            opponent = A[p][-1] - 1
            if A[opponent][-1] - 1 == p:
                if p < opponent:
                    games.add((p, opponent))
                else:
                    games.add((opponent, p))
    return(games)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    A[i].reverse()

games = list_up_next_games(list(range(N)))

done_games = []
days = 0
while games:
    days += 1
    players = []
    for game in games:
        done_games.append(game)
        A[game[0]].pop()
        A[game[1]].pop()
        players.append(game[0])
        players.append(game[1])
    games = list_up_next_games(players)

if len(done_games) == N*(N-1)//2:
    print(days)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
def scheduler(N, A):
    days = 0
    while True:
        games = []
        no_player = True
        for i in range(N):
            if len(A[i]):
                no_player = False
                oppo = A[i][-1] -1
                if A[oppo][-1] - 1 == i:
                    games.append(i)
        if no_player:
            break
        days += 1
        if len(games) == 0:
            return(-1)
        for game in games:
            A[game].pop()
    return(days)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

print(scheduler(N, A))

Fixed submission:
def list_up_next_games(players):
    games = set()
    for p in players:
        if A[p]:
            opponent = A[p][-1] - 1
            if A[opponent][-1] - 1 == p:
                if p < opponent:
                    games.add((p, opponent))
                else:
                    games.add((opponent, p))
    return(games)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    A[i].reverse()

games = list_up_next_games(list(range(N)))

done_games = []
days = 0
while games:
    days += 1
    players = []
    for game in games:
        done_games.append(game)
        A[game[0]].pop()
        A[game[1]].pop()
        players.append(game[0])
        players.append(game[1])
    games = list_up_next_games(players)

if len(done_games) == N*(N-1)//2:
    print(days)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
def scheduler(N, A):
    days = 0
    while True:
        games = []
        no_player = True
        for i in range(N):
            if len(A[i]):
                no_player = False
                oppo = A[i][-1] -1
                if A[oppo][-1] - 1 == i:
                    games.append(i)
        if no_player:
            break
        days += 1
        if len(games) == 0:
            return(-1)
        for game in games:
            A[game].pop()
    return(days)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

print(scheduler(N, A))

Fixed submission:
def list_up_next_games(players):
    games = set()
    for p in players:
        if A[p]:
            opponent = A[p][-1] - 1
            if A[opponent][-1] - 1 == p:
                if p < opponent:
                    games.add((p, opponent))
                else:
                    games.add((opponent, p))
    return(games)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    A[i].reverse()

games = list_up_next_games(list(range(N)))

done_games = []
days = 0
while games:
    days += 1
    players = []
    for game in games:
        done_games.append(game)
        A[game[0]].pop()
        A[game[1]].pop()
        players.append(game[0])
        players.append(game[1])
    games = list_up_next_games(players)

if len(done_games) == N*(N-1)//2:
    print(days)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
def scheduler(N, A):
    days = 0
    while True:
        games = []
        no_player = True
        for i in range(N):
            if len(A[i]):
                no_player = False
                oppo = A[i][0] -1
                if A[oppo][0] - 1 == i:
                    games.append(i)
        if no_player:
            break
        days += 1
        if len(games) == 0:
            return(-1)
        for game in games:
            del A[game][0]
    return(days)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

print(scheduler(N, A))

Fixed submission:
def list_up_next_games(players):
    games = set()
    for p in players:
        if A[p]:
            opponent = A[p][-1] - 1
            if A[opponent][-1] - 1 == p:
                if p < opponent:
                    games.add((p, opponent))
                else:
                    games.add((opponent, p))
    return(games)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    A[i].reverse()

games = list_up_next_games(list(range(N)))

done_games = []
days = 0
while games:
    days += 1
    players = []
    for game in games:
        done_games.append(game)
        A[game[0]].pop()
        A[game[1]].pop()
        players.append(game[0])
        players.append(game[1])
    games = list_up_next_games(players)

if len(done_games) == N*(N-1)//2:
    print(days)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
def scheduler(N, A):
    days = 0
    while True:
        games = []
        no_player = True
        for i in range(N):
            if len(A[i]):
                no_player = False
                oppo = A[i][0] -1
                if A[oppo][0] - 1 == i:
                    games.append(i)
        if no_player:
            break
        days += 1
        if len(games) == 0:
            return(-1)
        for game in games:
            del A[game][0]
    return(days)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

print(scheduler(N, A))

Fixed submission:
def list_up_next_games(players):
    games = set()
    for p in players:
        if A[p]:
            opponent = A[p][-1] - 1
            if A[opponent][-1] - 1 == p:
                if p < opponent:
                    games.add((p, opponent))
                else:
                    games.add((opponent, p))
    return(games)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    A[i].reverse()

games = list_up_next_games(list(range(N)))

done_games = []
days = 0
while games:
    days += 1
    players = []
    for game in games:
        done_games.append(game)
        A[game[0]].pop()
        A[game[1]].pop()
        players.append(game[0])
        players.append(game[1])
    games = list_up_next_games(players)

if len(done_games) == N*(N-1)//2:
    print(days)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
def scheduler(N, A):
    days = 0
    while len([a for a in A if len(a)]):
        days += 1
        game = [0 for i in range(N)]
        for i in range(N):
            if len(A[i]):
                oppo = A[i][0] -1
                if A[oppo][0] - 1 == i:
                    game[i] = 1
        no_game = True
        for i in range(N):
            if game[i] == 1:
                del A[i][0]
                no_game = False
        if no_game:
            return(-1)
    return(days)


N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

print(scheduler(N, A))

Fixed submission:
def list_up_next_games(players):
    games = set()
    for p in players:
        if A[p]:
            opponent = A[p][-1] - 1
            if A[opponent][-1] - 1 == p:
                if p < opponent:
                    games.add((p, opponent))
                else:
                    games.add((opponent, p))
    return(games)

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    A[i].reverse()

games = list_up_next_games(list(range(N)))

done_games = []
days = 0
while games:
    days += 1
    players = []
    for game in games:
        done_games.append(game)
        A[game[0]].pop()
        A[game[1]].pop()
        players.append(game[0])
        players.append(game[1])
    games = list_up_next_games(players)

if len(done_games) == N*(N-1)//2:
    print(days)
else:
    print(-1)