41642023-03-15 18:22:45kristofElágazás nélküli úton levő települések (50 pont)python3Runtime error 23/50137ms23468 KiB
from collections import defaultdict

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



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
base23/50
1Accepted0/019ms11972 KiB
2Accepted0/0134ms20888 KiB
3Runtime error0/220ms12540 KiB
4Wrong answer0/219ms12880 KiB
5Accepted2/219ms13032 KiB
6Runtime error0/219ms13232 KiB
7Accepted2/219ms13460 KiB
8Accepted2/229ms14424 KiB
9Accepted2/237ms15296 KiB
10Accepted2/250ms16260 KiB
11Accepted2/276ms18412 KiB
12Accepted2/278ms18492 KiB
13Wrong answer0/327ms14696 KiB
14Runtime error0/332ms15428 KiB
15Runtime error0/335ms15784 KiB
16Runtime error0/337ms16588 KiB
17Runtime error0/368ms18836 KiB
18Runtime error0/371ms19136 KiB
19Wrong answer0/386ms20108 KiB
20Accepted3/3129ms22936 KiB
21Accepted3/3134ms23208 KiB
22Accepted3/3137ms23468 KiB