Problem p02902 (python)

Fixed submission:
import sys
from collections import deque
# https://atcoder.jp/contests/abc142/submissions/7764934
# ほぼまんま。


def main():
    N, M = map(int, input().split())
    E = [set() for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        E[a-1].add(b-1)

    # スタート地点は全て試す
    for start in range(N):
        visited = [0]*N
        Q = deque([(start, {start})])
        append, popleft = Q.append, Q.popleft
        visited[start] = 1

        while Q:
            v, route = popleft()
            # これまで通ってきた頂点と行き先の積集合
            s = route & E[v]
            # sが空集合(ループでない)か、行き先に始点のみが含まれる場合は継続
            # 行き先に始点以外の場所が含まれるなら、もっと良い始点の選び方がある。
            # if not (not s or (s == {start})):
            if s and s != {start}:
                continue
            for dest in E[v]:
                if dest == start:
                    print(len(route))
                    print(*[n+1 for n in route], sep="\n")
                    return
                if visited[dest]:
                    continue
                visited[dest] = 1
                append((dest, route | {dest}))
    print(-1)
    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
INF = float("inf")


def Topological_sorting(E):
    N = len(E)
    in_deg = [0]*N
    for i in range(N):        # ２重ループでO(E)
        for to_ in E[i]:
            in_deg[to_] += 1

    curr = 0
    ans = [v for v in range(N) if in_deg[v] == 0]

    while curr < len(ans):                  # O(V+E)
        v = ans[curr]
        for t in E[v]:
            in_deg[t] -= 1
            if in_deg[t] == 0:
                ans.append(t)
        curr += 1
    if len(ans) != N:
        return False, ans
    return True, ans


def main():
    N, M = map(int, input().split())
    E = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        E[a-1].append(b-1)

    # 入次数、出次数をカウントして、
    # 誘導部分グラフにならない場所を削っていく
    not_looped, ans = Topological_sorting(E)
    if not_looped:
        print(-1)               # DAGなら誘導部分グラフは存在していない
        return
    # 存在するなら、単に閉路を見つければ良い
    in_deg = [0]*N
    for i in range(N):
        for to in E[i]:
            in_deg[to] += 1

    # 深さ優先探索をする
    curr = 0
    cand = []
    while not cand:
        cand = [v for v in range(N) if in_deg[v] == curr]
        curr += 1
    for v in cand:
        prev = [-1]*N
        seen = [-1]*N
        seen[v] = 0
        q = [v]
        head = 0
        while q:
            node = q[head]
            head += 1
            seen[node] = 1
            for u in E[node]:
                if seen[u] == 1:
                    # ループだ
                    prev[u] = node
                    w = node
                    ans.append(node+1)
                    while node != prev[w]:
                        ans.append(prev[w]+1)
                        w = prev[w]
                    print(len(ans))
                    print("\n".join(map(str, ans)))
                    return
                if seen[u] == 0:
                    continue
                q.append(u)
                prev[u] = node
                seen[u] = 0
    print(-1)


if __name__ == '__main__':
    main()