89492024-02-07 17:59:50NagyLeoA lehető legkevesebb metróval utazás (40 pont)python3Runtime error 6/4097ms67320 KiB
def main3():
    N, M, Dep, Arr = map(int, input().split())
    metro = []
    Dep_index = set()
    Arr_index = set()
    for i in range(N):
        asd = list(map(int, input().split()))

        metro.append(set(asd[1:]))
        if Dep in metro[i]:
            Dep_index.add(i)
        if Arr in metro[i]:
            Arr_index.add(i)
        if i in Arr_index and i in Dep_index:
            print(1)
            print(i + 1)
            return

    graph = {}

    for i in range(N - 1):
        for j in range(i + 1, N):
            if not metro[i].isdisjoint(metro[j]):
                try:
                    graph[i].append(j)
                except:
                    graph[i] = [j]

    touched = Dep_index.copy()
    current_res = [0] * N
    for i in Dep_index:
        current_res[i] = [i]
    while Dep_index:
        tmp = []
        for i in Dep_index:
            for j in graph[i]:
                if j not in touched:
                    tmp.append(j)
                    touched.add(j)
                    current_res[j] = current_res[i] + [j]
                    if j in Arr_index:
                        print(len(current_res[j]))
                        [print(x + 1, end=" ") for x in current_res[j]]
                        return
        Dep_index = tmp

    print("-1")


main3()

SubtaskSumTestVerdictTimeMemory
base6/40
1Accepted0/017ms11396 KiB
2Runtime error0/041ms14308 KiB
3Accepted2/217ms12088 KiB
4Accepted2/217ms12456 KiB
5Accepted2/217ms12280 KiB
6Runtime error0/217ms12408 KiB
7Runtime error0/218ms12800 KiB
8Runtime error0/220ms13640 KiB
9Runtime error0/223ms14932 KiB
10Runtime error0/223ms14000 KiB
11Runtime error0/220ms13888 KiB
12Runtime error0/250ms16772 KiB
13Runtime error0/250ms16988 KiB
14Runtime error0/243ms16600 KiB
15Runtime error0/297ms67320 KiB
16Runtime error0/296ms67100 KiB
17Runtime error0/287ms67148 KiB
18Runtime error0/297ms66852 KiB
19Runtime error0/237ms15688 KiB
20Runtime error0/239ms16096 KiB
21Runtime error0/232ms15528 KiB
22Runtime error0/243ms17100 KiB