91692024-02-16 18:52:53NagyLeoTestnevelés órapython3Runtime error 7/50943ms82716 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[::-1] in result:
            print(*i[::-1], end=" ")


solv()
SubtaskSumTestVerdictTimeMemory
base7/50
1Accepted0/017ms11432 KiB
2Runtime error0/018ms12072 KiB
3Runtime error0/0467ms48964 KiB
4Runtime error0/217ms12300 KiB
5Runtime error0/317ms12420 KiB
6Runtime error0/317ms12604 KiB
7Runtime error0/317ms12576 KiB
8Accepted1/117ms12748 KiB
9Runtime error0/317ms13116 KiB
10Runtime error0/321ms13376 KiB
11Runtime error0/325ms13844 KiB
12Accepted1/121ms13528 KiB
13Accepted2/225ms13944 KiB
14Runtime error0/320ms13452 KiB
15Accepted1/1462ms47532 KiB
16Runtime error0/3365ms55600 KiB
17Runtime error0/5224ms66892 KiB
18Runtime error0/1555ms80504 KiB
19Accepted2/2458ms48740 KiB
20Runtime error0/3943ms82348 KiB
21Runtime error0/4757ms82716 KiB
22Runtime error0/4767ms82616 KiB