91642024-02-16 18:25:28NagyLeoTestnevelés órapython3Runtime error 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()
SubtaskSumTestVerdictTimeMemory
base30/50
1Accepted0/017ms11440 KiB
2Accepted0/017ms11848 KiB
3Runtime error0/0612ms132812 KiB
4Accepted2/217ms12412 KiB
5Accepted3/318ms12504 KiB
6Accepted3/317ms12716 KiB
7Accepted3/317ms12944 KiB
8Time limit exceeded0/11.103s58828 KiB
9Accepted3/318ms13116 KiB
10Accepted3/352ms15200 KiB
11Accepted3/390ms15884 KiB
12Time limit exceeded0/11.052s7760 KiB
13Accepted2/2143ms15956 KiB
14Accepted3/341ms15236 KiB
15Runtime error0/1674ms130704 KiB
16Runtime error0/3537ms130524 KiB
17Accepted5/5248ms57900 KiB
18Runtime error0/1495ms130440 KiB
19Runtime error0/2597ms130396 KiB
20Runtime error0/3428ms130512 KiB
21Runtime error0/4423ms130432 KiB
22Runtime error0/4476ms130140 KiB