41672023-03-15 18:33:36kristofElágazás nélküli úton levő települések (50 pont)python3Runtime error 31/50141ms23716 KiB
from collections import defaultdict

def elerheto(honnan, mostani):
    if mostani in beenThere:
        return
    kiElerheto.append(mostani)
    beenThere.append(mostani)
    if len(graf[mostani]) <= 2:
        if graf[mostani][0] != honnan:
            elerheto(mostani, graf[mostani][0])
        else:
            elerheto(mostani, graf[mostani][1])
    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/020ms11836 KiB
2Accepted0/0141ms20860 KiB
3Runtime error0/220ms12440 KiB
4Accepted2/219ms12488 KiB
5Accepted2/219ms12708 KiB
6Runtime error0/219ms12908 KiB
7Accepted2/220ms13488 KiB
8Accepted2/229ms14128 KiB
9Accepted2/237ms15124 KiB
10Accepted2/250ms16016 KiB
11Accepted2/278ms18020 KiB
12Accepted2/276ms18196 KiB
13Accepted3/327ms14556 KiB
14Runtime error0/332ms15316 KiB
15Runtime error0/337ms15840 KiB
16Runtime error0/337ms16188 KiB
17Runtime error0/370ms18380 KiB
18Runtime error0/371ms19036 KiB
19Accepted3/386ms20364 KiB
20Accepted3/3133ms22972 KiB
21Accepted3/3138ms23716 KiB
22Accepted3/3136ms23584 KiB