91672024-02-16 18:49:38NagyLeoTestnevelés órapython3Time limit exceeded 43/501.011s82184 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 = len(zero)
    while zero:
        tmp = []
        for i in zero:
            for j in graph[i]:
                indeg[j] -= 1
                if indeg[j] == 0:
                    tmp.append(j)
                    seen += 1
        if tmp:
            result.append(tmp[:])
        zero = tmp

    # print(result,graph)
    if 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
base43/50
1Accepted0/018ms11408 KiB
2Accepted0/017ms11456 KiB
3Accepted0/0605ms49712 KiB
4Accepted2/217ms12476 KiB
5Accepted3/318ms12504 KiB
6Accepted3/317ms12424 KiB
7Accepted3/317ms12708 KiB
8Accepted1/117ms12720 KiB
9Accepted3/317ms12848 KiB
10Accepted3/325ms13404 KiB
11Accepted3/327ms13788 KiB
12Accepted1/125ms13536 KiB
13Accepted2/226ms13788 KiB
14Accepted3/321ms13740 KiB
15Accepted1/1603ms47092 KiB
16Accepted3/3537ms56276 KiB
17Accepted5/5277ms69828 KiB
18Accepted1/1805ms81576 KiB
19Accepted2/2620ms48244 KiB
20Time limit exceeded0/31.011s81728 KiB
21Time limit exceeded0/41.001s81916 KiB
22Accepted4/4888ms82184 KiB