89502024-02-07 18:00:10NagyLeoA lehető legkevesebb metróval utazás (40 pont)pypy3Runtime error 6/40116ms101388 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/061ms87472 KiB
2Runtime error0/0108ms97376 KiB
3Accepted2/250ms87620 KiB
4Accepted2/254ms87468 KiB
5Accepted2/257ms87684 KiB
6Runtime error0/282ms95224 KiB
7Runtime error0/281ms96696 KiB
8Runtime error0/287ms96544 KiB
9Runtime error0/289ms96488 KiB
10Runtime error0/290ms96492 KiB
11Runtime error0/282ms96496 KiB
12Runtime error0/2107ms97544 KiB
13Runtime error0/2116ms97560 KiB
14Runtime error0/2104ms97352 KiB
15Runtime error0/290ms101388 KiB
16Runtime error0/290ms101004 KiB
17Runtime error0/290ms101168 KiB
18Runtime error0/2101ms101180 KiB
19Runtime error0/2101ms97392 KiB
20Runtime error0/298ms97364 KiB
21Runtime error0/297ms96920 KiB
22Runtime error0/2112ms97424 KiB