41752023-03-15 19:16:44kristofElágazás nélküli úton levő települések (50 pont)python3Wrong answer 39/50136ms23736 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(*sorted(kiElerheto))
SubtaskSumTestVerdictTimeMemory
base39/50
1Accepted0/020ms12060 KiB
2Accepted0/0136ms20996 KiB
3Wrong answer0/219ms12620 KiB
4Accepted2/220ms12744 KiB
5Accepted2/219ms12832 KiB
6Accepted2/220ms13180 KiB
7Accepted2/219ms13412 KiB
8Accepted2/229ms14256 KiB
9Accepted2/239ms15276 KiB
10Accepted2/250ms16260 KiB
11Accepted2/275ms18328 KiB
12Accepted2/279ms18436 KiB
13Accepted3/327ms14680 KiB
14Accepted3/334ms15480 KiB
15Wrong answer0/337ms16160 KiB
16Wrong answer0/352ms16744 KiB
17Accepted3/370ms19140 KiB
18Wrong answer0/375ms19428 KiB
19Accepted3/390ms20324 KiB
20Accepted3/3133ms23200 KiB
21Accepted3/3136ms23736 KiB
22Accepted3/3134ms23696 KiB