Problem p02902 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    loop.reverse()
    t = loop[-1]
    loop = loop[loop.index(t):]
    ll = set(loop)
    flag = True
    while flag:
        for i, v in enumerate(loop[:-1]):
            for j in l[v]:
                if j in ll and loop[i + 1] != j:
                    print(i, v,j)
                    if loop.index(j) == 0:
                        loop = loop[:i+1]
                    else:
                        loop = loop[:i+1] + loop[loop.index(j):]
                    print(loop)
                    ll = set(loop)
                    break
        else:
            flag = False
    loop = set(loop)
    print(len(loop))
    for i in loop:
        print(i+1)
    return
main()

Buggy submission (verdict: Runtime Error):
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
import copy
import numpy as np
def cyclic(n, l, v):
    ans = []
    v.add(n)
    for i in  l[n]:
        if i in v:
            return True, v
    for i in l[n]:
        v2 = copy.deepcopy(v)
        t, vv = cyclic(i, l, v2)
        #v.remove(i)
        if t:
            ans.append(vv)
        if ans:
            return True, ans
    return False, v
def main():
    N, M  = map(int, input().split())
    l = [[] for _ in range(N)]
    r = []
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    for i in range(N):
        v = set()
        t, vv = cyclic(i, l, v)
        if t:
            r.extend(np.ndarray.flatten(np.array(vv)).tolist())
    if r:
        m = 100000
        rr = None
        for i in r:
            if m > len(i):
                m = len(i)
                rr = i
        print(m)
        for i in rr:
            print(i+1)
    else:
        print(-1)
    return -1
main()

Buggy submission (verdict: Runtime Error):
import copy
import numpy as np
def cyclic(n, l, v):
    ans = []
    v.add(n)
    for i in  l[n]:
        if i in v:
            return True, v
    for i in l[n]:
        v2 = copy.deepcopy(v)
        t, vv = cyclic(i, l, v2)
        #v.remove(i)
        if t:
            ans.append(vv)
        if ans:
            return True, ans
    return False, v
def main():
    N, M  = map(int, input().split())
    l = [[] for _ in range(N)]
    r = []
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    for i in range(N):
        v = set()
        t, vv = cyclic(i, l, v)
        if t:
            r.extend(np.ndarray.flatten(np.array(vv)).tolist())
    if r:
        m = 100000
        rr = None
        for i in r:
            if m > len(i):
                m = len(i)
                rr = i
        print(m)
        for i in rr:
            print(i+1)
    print(-1)
    return -1
main()