41712023-03-15 19:09:57kristofElágazás nélküli úton levő települések (50 pont)python3Wrong answer 0/50136ms23676 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 0


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((kiElerheto))
SubtaskSumTestVerdictTimeMemory
base0/50
1Wrong answer0/019ms12076 KiB
2Wrong answer0/0136ms20968 KiB
3Runtime error0/220ms12600 KiB
4Wrong answer0/219ms12800 KiB
5Wrong answer0/220ms12984 KiB
6Runtime error0/219ms13048 KiB
7Wrong answer0/220ms12948 KiB
8Wrong answer0/229ms13684 KiB
9Wrong answer0/237ms14816 KiB
10Wrong answer0/250ms16236 KiB
11Wrong answer0/278ms17916 KiB
12Wrong answer0/276ms18160 KiB
13Wrong answer0/327ms14712 KiB
14Runtime error0/332ms15496 KiB
15Runtime error0/335ms16200 KiB
16Runtime error0/337ms16612 KiB
17Runtime error0/368ms18704 KiB
18Runtime error0/371ms19012 KiB
19Wrong answer0/387ms20404 KiB
20Wrong answer0/3130ms23348 KiB
21Wrong answer0/3136ms23676 KiB
22Wrong answer0/3134ms23548 KiB