Problem p02925 (python)

Fixed submission:
import math
import collections
import heapq
from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n = int(input())
    maxv = n*(n-1)//2
    a = [list(map(int, input().split())) for i in range(n)]
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
    v = 0
    Id = [[0 for i in range(n)] for j in range(n)]
    for i in range(n):
        for j in range(n):
            if i < j:
                Id[i][j] = v
                v += 1

    def toId(i, j):
        if i > j:
            i, j = j, i
        return Id[i][j]

    to = [[] for i in range(maxv)]
    for i in range(n):
        for j in range(n - 1):
            a[i][j] = toId(i, a[i][j])
        for j in range(n - 2):
            to[a[i][j+1]].append(a[i][j])

    visited = [False for i in range(maxv)]
    calculated = [False for i in range(maxv)]
    dp = [0 for i in range(maxv)]

    def dfs(v):
        if visited[v]:
            if not calculated[v]:
                return -1
            return dp[v]
        visited[v] = True
        dp[v] = 1
        for u in to[v]:
            ret = dfs(u)
            if ret == -1:
                return -1
            dp[v] = max(dp[v], ret + 1)
        calculated[v] = True
        return dp[v]

    ans = 0
    for i in range(v):
        ret = dfs(i)
        if ret == -1:  # detected cycle
            print(-1)
            exit()
        ans = max(ans, ret)
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import math
import collections
import heapq
from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n = int(input())
    maxv = n*(n-1)//2
    a = [list(map(lambda x:x-1, list(map(int, input().split()))))for i in range(n)]


    # 試合を頂点とする(ex 1-2,2-4 : i < j)
    v = 0  # 頂点数
    # 頂点にIDをふる
    Id = [[-1 for i in range(n)] for j in range(n)]
    for i in range(n):
        for j in range(n):
            if i < j:
                Id[i][j] = v
                Id[j][i] = v
                v += 1

    # 頂点間の辺を貼る
    e = [[] for i in range(maxv)]
    for i in range(n):
        # a[i][j]にIDを代入する Id[1][3]=1 => a[1][3] = a[3][1] = 1
        for j in range(n - 1):
            a[i][j] = Id[i][a[i][j]]

        # 辺をはる
        for j in range(n - 2):
            e[a[i][j]].append(a[i][j+1])


    calculated = [False for i in range(maxv)]
    dp = [-1 for i in range(maxv)]
    def dfs(v):
        if dp[v] != -1:
            if not calculated[v]:
                return -1
            return dp[v]
        dp[v] = 1
        for u in e[v]:
            ret = dfs(u)
            if ret == -1:
                return -1
            dp[v] = max(dp[v], ret + 1)
        calculated[v] = True　#帰りがけ
        return dp[v]
    

    #トポロジカルソート
    ans = 0
    for i in range(v):
        ret = dfs(i)
        if ret == -1:  # サイクル検知
            print(-1)
            exit()
        ans = max(ans, ret)
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import math
import collections
import heapq
from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n = int(input())
    maxv = n*(n-1)//2
    a = [list(map(lambda x:x-1, list(map(int, input().split()))))
         for i in range(n)]


    # 試合を頂点とする(ex 1-2,2-4 : i < j)
    v = 0  # 頂点数
    # 頂点にIDをふる
    Id = [[-1 for i in range(n)] for j in range(n)]
    for i in range(n):
        for j in range(n):
            if i < j:
                Id[i][j] = v
                Id[j][i] = v
                v += 1

    # 頂点間の辺を貼る
    e = [[] for i in range(maxv)]
    for i in range(n):
        # a[i][j]にIDを代入する Id[1][3]=1 => a[1][3] = a[3][1] = 1
        for j in range(n - 1):
            a[i][j] = Id[i][a[i][j]]

        # 辺をはる
        for j in range(n - 2):
            e[a[i][j]].append(a[i][j+1])


    calculated = [False for i in range(maxv)]
    dp = [-1 for i in range(maxv)]
    def dfs(v):
        if dp[v] != -1:
            if not calculated[v]:
                return -1
            return dp[v]
        dp[v] = 1
        for u in e[v]:
            ret = dfs(u)
            if ret == -1:
                return -1
            dp[v] = max(dp[v], ret + 1)
        calculated[v] = True　#帰りがけ
        return dp[v]
    

    #トポロジカルソート
    ans = 0
    for i in range(v):
        ret = dfs(i)
        if ret == -1:  # サイクル検知
            print(-1)
            exit()
        ans = max(ans, ret)
    print(ans)


if __name__ == '__main__':
    main()