41782023-03-15 21:40:21kristofElágazás nélküli úton levő települések (50 pont)python3Wrong answer 4/50136ms22604 KiB
from collections import defaultdict

def elerheto(honnan, mostani):
    if mostani not in kiElerheto:
        kiElerheto.add(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 = set()
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:
        kiElerheto.add(i)
        elerheto(i, graf[i][0])



if len(kiElerheto) == 0:
    print(0)
else:
    print(len(kiElerheto))
    print(*sorted(kiElerheto))
SubtaskSumTestVerdictTimeMemory
base4/50
1Wrong answer0/020ms11912 KiB
2Wrong answer0/0134ms20984 KiB
3Accepted2/219ms12484 KiB
4Wrong answer0/219ms12296 KiB
5Accepted2/219ms12532 KiB
6Wrong answer0/219ms12756 KiB
7Wrong answer0/219ms12908 KiB
8Wrong answer0/228ms13676 KiB
9Wrong answer0/237ms14648 KiB
10Wrong answer0/250ms15908 KiB
11Wrong answer0/278ms17840 KiB
12Wrong answer0/279ms17796 KiB
13Wrong answer0/327ms14048 KiB
14Wrong answer0/332ms14712 KiB
15Wrong answer0/337ms15340 KiB
16Wrong answer0/339ms16064 KiB
17Wrong answer0/367ms18124 KiB
18Wrong answer0/372ms18440 KiB
19Wrong answer0/390ms19420 KiB
20Wrong answer0/3136ms22248 KiB
21Wrong answer0/3136ms22604 KiB
22Wrong answer0/3135ms22440 KiB