41722023-03-15 19:12:51kristofElágazás nélküli úton levő települések (50 pont)python3Wrong answer 39/50141ms23564 KiB
from collections import defaultdict

def elerheto(honnan, mostani):

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


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
base39/50
1Accepted0/020ms12016 KiB
2Accepted0/0136ms21048 KiB
3Wrong answer0/220ms12432 KiB
4Accepted2/219ms12716 KiB
5Accepted2/221ms12888 KiB
6Accepted2/220ms12984 KiB
7Accepted2/220ms12940 KiB
8Accepted2/230ms14028 KiB
9Accepted2/239ms14848 KiB
10Accepted2/250ms15860 KiB
11Accepted2/278ms18128 KiB
12Accepted2/279ms18548 KiB
13Accepted3/328ms14260 KiB
14Accepted3/334ms15356 KiB
15Wrong answer0/337ms15764 KiB
16Wrong answer0/352ms16700 KiB
17Accepted3/371ms18780 KiB
18Wrong answer0/375ms19140 KiB
19Accepted3/392ms20172 KiB
20Accepted3/3137ms22972 KiB
21Accepted3/3141ms23456 KiB
22Accepted3/3141ms23564 KiB