91642024-02-16 18:25:28NagyLeoTestnevelés órapython3Futási hiba 30/501.103s132812 KiB
def solv():
    N, K = map(int, input().split())
    graph = {}
    inverted_graph = {}

    for i in range(K):
        a, b = map(int, input().split())
        try:
            graph[a].add(b)
        except:
            graph[a] = {b}
        try:
            inverted_graph[b].add(a)
        except:
            inverted_graph[b] = {a}

    parents = []
    #   a --> b --> 2 <-- c
    #     \-> d
    for i in range(1, N + 1):
        if i not in graph:
            parents.append(i)
    # print(parents)
    result = [parents[:]]
    seen = set(parents)
    while graph:
        for i in parents:
            if i not in inverted_graph:
                continue
            for j in inverted_graph[i]:
                graph[j].remove(i)
                if len(graph[j]) == 0:
                    graph.pop(j)
        parents = []
        for i in range(1, N + 1):
            if i not in graph and i not in seen:
                parents.append(i)
                seen.add(i)
        result.append(parents[:])
    # print(result,graph)
    if len(seen) != N:
        print("0")
        return
    if len(result) == N:
        print("1")
        for i in result[::-1]:
            print(i[0], end=" ")
    else:
        # result=[ [1,2,3], [4,5]]
        print("2")
        for i in result[::-1]:
            print(*i, end=" ")
        print()
        for i in result[::-1]:
            print(*i[::-1], end=" ")




solv()
RészfeladatÖsszpontTesztVerdiktIdőMemória
base30/50
1Elfogadva0/017ms11440 KiB
2Elfogadva0/017ms11848 KiB
3Futási hiba0/0612ms132812 KiB
4Elfogadva2/217ms12412 KiB
5Elfogadva3/318ms12504 KiB
6Elfogadva3/317ms12716 KiB
7Elfogadva3/317ms12944 KiB
8Időlimit túllépés0/11.103s58828 KiB
9Elfogadva3/318ms13116 KiB
10Elfogadva3/352ms15200 KiB
11Elfogadva3/390ms15884 KiB
12Időlimit túllépés0/11.052s7760 KiB
13Elfogadva2/2143ms15956 KiB
14Elfogadva3/341ms15236 KiB
15Futási hiba0/1674ms130704 KiB
16Futási hiba0/3537ms130524 KiB
17Elfogadva5/5248ms57900 KiB
18Futási hiba0/1495ms130440 KiB
19Futási hiba0/2597ms130396 KiB
20Futási hiba0/3428ms130512 KiB
21Futási hiba0/4423ms130432 KiB
22Futási hiba0/4476ms130140 KiB