41692023-03-15 18:46:24kristofElágazás nélküli úton levő települések (50 pont)python3Runtime error 31/50137ms22700 KiB
from collections import defaultdict

def elerheto(honnan, mostani):

    if mostani not in kiElerheto:
    
        kiElerheto.append(mostani)
        if len(graf[mostani]) <= 2:
            if graf[mostani][0] != honnan:
                return elerheto(mostani, graf[mostani][0])
            else:
                return elerheto(mostani, graf[mostani][1])
        return
    else:
        return


beenThere = []

n, m = list(map(int, input().split()))
graf = defaultdict(list)

kiElerheto = []
kiSzamlalo = 0


#todo Is there a way without array?
arr = [0 for i in range(n)]

for i in range(m):
    honnan1, hova = list(map(int, input().split()))

    graf[honnan1].append(hova)
    graf[hova].append(honnan1)

    arr[honnan1-1] += 1
    arr[hova-1] += 1

zsakfalvak = []
zsakfalvak = [i+1 for i, value in enumerate(arr) if value == 1]

for i in zsakfalvak:
    if len(graf[i]) == 1:
        elerheto(i, graf[i][0])


print(len(kiElerheto))
print(*sorted(kiElerheto))
SubtaskSumTestVerdictTimeMemory
base31/50
1Accepted0/019ms12004 KiB
2Accepted0/0133ms20780 KiB
3Runtime error0/220ms12264 KiB
4Accepted2/219ms12516 KiB
5Accepted2/219ms12460 KiB
6Runtime error0/219ms12900 KiB
7Accepted2/220ms12912 KiB
8Accepted2/228ms13636 KiB
9Accepted2/237ms14428 KiB
10Accepted2/248ms15388 KiB
11Accepted2/275ms17532 KiB
12Accepted2/276ms17436 KiB
13Accepted3/327ms13948 KiB
14Runtime error0/332ms14492 KiB
15Runtime error0/335ms14972 KiB
16Runtime error0/337ms15708 KiB
17Runtime error0/370ms17884 KiB
18Runtime error0/372ms18448 KiB
19Accepted3/386ms19476 KiB
20Accepted3/3134ms22120 KiB
21Accepted3/3137ms22700 KiB
22Accepted3/3136ms22552 KiB