91702024-02-16 18:55:34NagyLeoTestnevelés órapython3Accepted 50/50902ms82708 KiB
from sys import stdin, stdout
input=stdin.readline


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)
        if tmp:
            result.append(tmp[:])
            seen+=len(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
base50/50
1Accepted0/018ms11288 KiB
2Accepted0/017ms11796 KiB
3Accepted0/0490ms49620 KiB
4Accepted2/217ms11948 KiB
5Accepted3/317ms12348 KiB
6Accepted3/318ms12672 KiB
7Accepted3/317ms12588 KiB
8Accepted1/118ms13000 KiB
9Accepted3/318ms13244 KiB
10Accepted3/323ms13556 KiB
11Accepted3/325ms13908 KiB
12Accepted1/121ms13396 KiB
13Accepted2/224ms14232 KiB
14Accepted3/320ms14328 KiB
15Accepted1/1418ms47776 KiB
16Accepted3/3404ms56832 KiB
17Accepted5/5266ms70432 KiB
18Accepted1/1646ms82368 KiB
19Accepted2/2425ms49036 KiB
20Accepted3/3786ms82584 KiB
21Accepted4/4783ms82556 KiB
22Accepted4/4902ms82708 KiB