89532024-02-07 18:05:43NagyLeoA lehető legkevesebb metróval utazás (40 pont)pypy3Futási hiba 32/4097ms100880 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()
RészfeladatÖsszpontTesztVerdiktIdőMemória
base32/40
1Elfogadva0/057ms87904 KiB
2Elfogadva0/076ms95720 KiB
3Elfogadva2/254ms87808 KiB
4Elfogadva2/250ms87740 KiB
5Elfogadva2/246ms87580 KiB
6Elfogadva2/250ms87772 KiB
7Elfogadva2/254ms94800 KiB
8Elfogadva2/261ms94516 KiB
9Elfogadva2/257ms94844 KiB
10Elfogadva2/254ms94964 KiB
11Elfogadva2/265ms94880 KiB
12Elfogadva2/282ms96232 KiB
13Elfogadva2/290ms96176 KiB
14Elfogadva2/282ms96184 KiB
15Futási hiba0/297ms100880 KiB
16Futási hiba0/294ms100612 KiB
17Futási hiba0/286ms100700 KiB
18Futási hiba0/296ms100692 KiB
19Elfogadva2/285ms96044 KiB
20Elfogadva2/275ms95960 KiB
21Elfogadva2/283ms95876 KiB
22Elfogadva2/286ms96372 KiB