41662023-03-15 18:29:57kristofElágazás nélküli úton levő települések (50 pont)python3Runtime error 23/50143ms23244 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])
    return 0



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/019ms11976 KiB
2Accepted0/0143ms21200 KiB
3Runtime error0/219ms12468 KiB
4Wrong answer0/219ms12356 KiB
5Accepted2/219ms12472 KiB
6Runtime error0/219ms12912 KiB
7Accepted2/220ms13140 KiB
8Accepted2/229ms14176 KiB
9Accepted2/239ms14764 KiB
10Accepted2/250ms15828 KiB
11Accepted2/278ms18036 KiB
12Accepted2/276ms17960 KiB
13Wrong answer0/326ms14148 KiB
14Runtime error0/332ms14768 KiB
15Runtime error0/337ms15316 KiB
16Runtime error0/337ms15952 KiB
17Runtime error0/371ms18200 KiB
18Runtime error0/374ms18748 KiB
19Wrong answer0/389ms19944 KiB
20Accepted3/3136ms22816 KiB
21Accepted3/3136ms23192 KiB
22Accepted3/3138ms23244 KiB