91682024-02-16 18:52:17NagyLeoTestnevelés órapython3Runtime error 7/50944ms82388 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/017ms11444 KiB
2Runtime error0/018ms11948 KiB
3Runtime error0/0435ms49452 KiB
4Runtime error0/217ms12364 KiB
5Runtime error0/318ms12628 KiB
6Runtime error0/318ms13060 KiB
7Runtime error0/317ms13016 KiB
8Accepted1/117ms13048 KiB
9Runtime error0/318ms12968 KiB
10Runtime error0/323ms13616 KiB
11Runtime error0/325ms13932 KiB
12Accepted1/123ms13768 KiB
13Accepted2/224ms14156 KiB
14Runtime error0/320ms14236 KiB
15Accepted1/1453ms47708 KiB
16Runtime error0/3421ms55824 KiB
17Runtime error0/5228ms66980 KiB
18Runtime error0/1616ms80576 KiB
19Accepted2/2469ms49072 KiB
20Runtime error0/3944ms82312 KiB
21Runtime error0/4898ms82388 KiB
22Runtime error0/4768ms82336 KiB