91662024-02-16 18:46:10NagyLeoTestnevelés órapython3Time limit exceeded 46/501.023s85384 KiB
def solv():
    N, K = map(int, input().split())
    graph = [[] for i in range(N + 1)]
    indeg = [0] * (N + 1)

    for i in range(K):
        a, b = map(int, input().split())
        graph[a].append(b)
        indeg[b] += 1

    zero = []
    #   a --> b --> 2 <-- c
    #     \-> d
    for i in range(1, N + 1):
        if indeg[i] == 0:
            zero.append(i)
    # print(parents)
    result = [zero[:]]
    seen = zero[:]
    while zero:
        tmp = []
        for i in zero:
            for j in graph[i]:
                indeg[j] -= 1
                if indeg[j] == 0:
                    tmp.append(j)
                    seen.append(j)
        if tmp:
            result.append(tmp[:])
        zero = tmp

    # print(result,graph)
    if len(seen) != N:
        print("0")
        return
    if len(result) == N:
        print("1")
        for i in result:
            print(*i, end=" ")
    else:
        # result=[ [1,2,3], [4,5]]
        print("2")
        for i in result:
            print(*i, end=" ")
        print()
        for i in result:
            print(*i[::-1], end=" ")


solv()
SubtaskSumTestVerdictTimeMemory
base46/50
1Accepted0/017ms11356 KiB
2Accepted0/017ms11836 KiB
3Accepted0/0619ms51040 KiB
4Accepted2/218ms12172 KiB
5Accepted3/318ms12484 KiB
6Accepted3/318ms12200 KiB
7Accepted3/318ms12488 KiB
8Accepted1/118ms12552 KiB
9Accepted3/318ms12468 KiB
10Accepted3/325ms13160 KiB
11Accepted3/327ms13404 KiB
12Accepted1/125ms13116 KiB
13Accepted2/226ms13828 KiB
14Accepted3/320ms14120 KiB
15Accepted1/1609ms48192 KiB
16Accepted3/3537ms58616 KiB
17Accepted5/5280ms73460 KiB
18Accepted1/1750ms85384 KiB
19Accepted2/2603ms49948 KiB
20Accepted3/3902ms84748 KiB
21Time limit exceeded0/41.023s84848 KiB
22Accepted4/4893ms85052 KiB