Problem p02925 (python)

Fixed submission:
def main():
    import sys
    sys.setrecursionlimit(10**7)
    input = sys.stdin.readline
    n = int(input())
    l = [list(map(int,input().split())) for i in range(n)]
    ans = 1
    count = [0]*n
    check = set()
    game = []
    for i in range(n):
        if count[i] >= n-1 or i in check:
            continue
        ind = l[i][count[i]]-1
        if ind in check:
            continue
        if l[ind].index(i+1) == count[ind]:
            check |= set([i,ind])
            game.append([i,ind])
            count[i] += 1
            count[ind] += 1
    while True:
        gamepre = game
        check = set()
        game = []
        for i,j in gamepre:
            ci = True
            cj = True
            if count[i] >= n-1 or i in check:
                ci = False
            if count[j] >= n-1 or j in check:
                cj = False
            if ci:
                ci = True
                ind = l[i][count[i]]-1
                if ind in check:
                    ci = False
                if ci and l[ind].index(i+1) == count[ind]:
                    check |= set([i,ind])
                    game.append([i,ind])
                    count[i] += 1
                    count[ind] += 1
            if cj:
                cj = True
                ind = l[j][count[j]]-1
                if ind in check:
                    cj = False
                if cj and l[ind].index(j+1) == count[ind]:
                    check |= set([j,ind])
                    game.append([j,ind])
                    count[j] += 1
                    count[ind] += 1
        if not check:
            if sum(count) != n*(n-1):
                print(-1)
                exit()
            else:
                print(ans)
                exit()
        ans += 1
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
n = int(input())
l = [list(map(int,input().split())) for i in range(n)]
v = n*(n-1)//2
E = [[] for i in range(v)]
dic = {}
c = 0
for i in range(n):
    for j in range(n-1):
        a,b = i,l[i][j]-1
        if a > b:
            a,b = b,a
        if (a,b) in dic:
            l[i][j] = dic[(a,b)]
        else:
            dic[(a,b)] = c
            l[i][j] = dic[(a,b)]
            c += 1
    for j in range(n-2):
        E[l[i][j]].append(l[i][j+1])

def dfs(i):
    if visit[i]:
        if dis[i] == -1:
            return -1
        else:
            return dis[i]
    visit[i] = True
    d = 1
    for nex in E[i]:
        res = dfs(nex)
        if res == -1:
            return -1
        d = max(d,res+1)

    dis[i] = d
    return d

visit = [False]*v
dis = [-1]*v
print(dfs(l[0][0]))

Buggy submission (verdict: Runtime Error):
n = int(input())
l = [list(map(int,input().split())) for i in range(n)]
v = n*(n-1)//2
E = [[] for i in range(v)]
dic = {}
c = 0
for i in range(n):
    for j in range(n-1):
        a,b = i,l[i][j]-1
        if a > b:
            a,b = b,a
        if (a,b) in dic:
            l[i][j] = dic[(a,b)]
        else:
            dic[(a,b)] = c
            l[i][j] = dic[(a,b)]
            c += 1
    for j in range(n-2):
        E[l[i][j]].append(l[i][j+1])

def dfs(i):
    if visit[i]:
        if dis[i] == -1:
            return -1
        else:
            return dis[i]
    visit[i] = True
    d = 1
    for nex in E[i]:
        res = dfs(nex)
        if res == -1:
            return -1
        d = max(d,res+1)

    dis[i] = d
    return d
ans = 0
done = []
for i in range(n):
    if l[i][0] in done:
        continue
    visit = [False]*v
    dis = [-1]*v
    count = dfs(l[i][0])
    if count == -1:
        print(-1)
        exit()
    else:
        ans = max(ans,count)
    done.append(l[i][0])
print(ans)