41792023-03-15 21:41:21kristofElágazás nélküli úton levő települések (50 pont)python3Wrong answer 31/50144ms23612 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])
        kiElerheto.remove(i)



if len(kiElerheto) == 0:
    print(0)
else:
    print(len(kiElerheto))
    print(*sorted(kiElerheto))
SubtaskSumTestVerdictTimeMemory
base31/50
1Accepted0/020ms12012 KiB
2Accepted0/0133ms21000 KiB
3Wrong answer0/220ms12268 KiB
4Accepted2/219ms12524 KiB
5Accepted2/219ms12900 KiB
6Wrong answer0/219ms13248 KiB
7Accepted2/219ms13112 KiB
8Accepted2/228ms13852 KiB
9Accepted2/237ms14552 KiB
10Accepted2/250ms15836 KiB
11Accepted2/278ms17720 KiB
12Accepted2/279ms17792 KiB
13Accepted3/327ms14460 KiB
14Wrong answer0/334ms15272 KiB
15Wrong answer0/337ms15676 KiB
16Wrong answer0/339ms16392 KiB
17Wrong answer0/371ms18772 KiB
18Wrong answer0/374ms18780 KiB
19Accepted3/386ms19924 KiB
20Accepted3/3129ms23068 KiB
21Accepted3/3144ms23612 KiB
22Accepted3/3134ms23436 KiB