91702024-02-16 18:55:34NagyLeoTestnevelés órapython3Elfogadva 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()
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/018ms11288 KiB
2Elfogadva0/017ms11796 KiB
3Elfogadva0/0490ms49620 KiB
4Elfogadva2/217ms11948 KiB
5Elfogadva3/317ms12348 KiB
6Elfogadva3/318ms12672 KiB
7Elfogadva3/317ms12588 KiB
8Elfogadva1/118ms13000 KiB
9Elfogadva3/318ms13244 KiB
10Elfogadva3/323ms13556 KiB
11Elfogadva3/325ms13908 KiB
12Elfogadva1/121ms13396 KiB
13Elfogadva2/224ms14232 KiB
14Elfogadva3/320ms14328 KiB
15Elfogadva1/1418ms47776 KiB
16Elfogadva3/3404ms56832 KiB
17Elfogadva5/5266ms70432 KiB
18Elfogadva1/1646ms82368 KiB
19Elfogadva2/2425ms49036 KiB
20Elfogadva3/3786ms82584 KiB
21Elfogadva4/4783ms82556 KiB
22Elfogadva4/4902ms82708 KiB