91662024-02-16 18:46:10NagyLeoTestnevelés órapython3Időlimit túllépés 46/501.023s85384 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 = zero[:]
    while zero:
        tmp = []
        for i in zero:
            for j in graph[i]:
                indeg[j] -= 1
                if indeg[j] == 0:
                    tmp.append(j)
                    seen.append(j)
        if tmp:
            result.append(tmp[:])
        zero = tmp

    # print(result,graph)
    if len(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
base46/50
1Elfogadva0/017ms11356 KiB
2Elfogadva0/017ms11836 KiB
3Elfogadva0/0619ms51040 KiB
4Elfogadva2/218ms12172 KiB
5Elfogadva3/318ms12484 KiB
6Elfogadva3/318ms12200 KiB
7Elfogadva3/318ms12488 KiB
8Elfogadva1/118ms12552 KiB
9Elfogadva3/318ms12468 KiB
10Elfogadva3/325ms13160 KiB
11Elfogadva3/327ms13404 KiB
12Elfogadva1/125ms13116 KiB
13Elfogadva2/226ms13828 KiB
14Elfogadva3/320ms14120 KiB
15Elfogadva1/1609ms48192 KiB
16Elfogadva3/3537ms58616 KiB
17Elfogadva5/5280ms73460 KiB
18Elfogadva1/1750ms85384 KiB
19Elfogadva2/2603ms49948 KiB
20Elfogadva3/3902ms84748 KiB
21Időlimit túllépés0/41.023s84848 KiB
22Elfogadva4/4893ms85052 KiB