89522024-02-07 18:05:06NagyLeoA lehető legkevesebb metróval utazás (40 pont)python3Runtime error 32/4098ms67420 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]
                try:
                    graph[j].append(i)
                except:
                    graph[j] = [i]

    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:
            if i not in graph:
                continue
            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
base32/40
1Accepted0/017ms11764 KiB
2Accepted0/041ms14668 KiB
3Accepted2/218ms12236 KiB
4Accepted2/217ms12164 KiB
5Accepted2/218ms12584 KiB
6Accepted2/217ms12280 KiB
7Accepted2/219ms13540 KiB
8Accepted2/219ms13948 KiB
9Accepted2/221ms15068 KiB
10Accepted2/223ms14244 KiB
11Accepted2/220ms13984 KiB
12Accepted2/250ms16660 KiB
13Accepted2/250ms16884 KiB
14Accepted2/243ms16804 KiB
15Runtime error0/298ms67220 KiB
16Runtime error0/286ms67420 KiB
17Runtime error0/297ms67128 KiB
18Runtime error0/290ms67380 KiB
19Accepted2/239ms15032 KiB
20Accepted2/239ms16268 KiB
21Accepted2/232ms15428 KiB
22Accepted2/243ms16476 KiB