41682023-03-15 18:45:21kristofElágazás nélküli úton levő települések (50 pont)python3Runtime error 31/50138ms23536 KiB
from collections import defaultdict

def elerheto(honnan, mostani):

    if mostani in kiElerheto:
        return
    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


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
base31/50
1Accepted0/020ms12068 KiB
2Accepted0/0138ms21056 KiB
3Runtime error0/219ms12324 KiB
4Accepted2/220ms12752 KiB
5Accepted2/220ms13376 KiB
6Runtime error0/220ms13188 KiB
7Accepted2/219ms13664 KiB
8Accepted2/228ms14332 KiB
9Accepted2/239ms14920 KiB
10Accepted2/252ms16060 KiB
11Accepted2/275ms18068 KiB
12Accepted2/279ms18240 KiB
13Accepted3/327ms14868 KiB
14Runtime error0/332ms15416 KiB
15Runtime error0/337ms16112 KiB
16Runtime error0/337ms16484 KiB
17Runtime error0/368ms18680 KiB
18Runtime error0/371ms19184 KiB
19Accepted3/389ms20128 KiB
20Accepted3/3130ms22968 KiB
21Accepted3/3137ms23536 KiB
22Accepted3/3138ms23492 KiB