41772023-03-15 21:36:29kristofElágazás nélküli úton levő települések (50 pont)python3Wrong answer 39/50137ms22680 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:
        elerheto(i, graf[i][0])



if len(kiElerheto) == 0:
    print(0)
else:
    print(len(kiElerheto))
    print(*sorted(kiElerheto))
SubtaskSumTestVerdictTimeMemory
base39/50
1Accepted0/019ms11904 KiB
2Accepted0/0137ms20876 KiB
3Wrong answer0/219ms12476 KiB
4Accepted2/219ms12644 KiB
5Accepted2/219ms12844 KiB
6Accepted2/219ms13032 KiB
7Accepted2/219ms12916 KiB
8Accepted2/228ms13816 KiB
9Accepted2/237ms14828 KiB
10Accepted2/250ms15784 KiB
11Accepted2/275ms17688 KiB
12Accepted2/278ms17772 KiB
13Accepted3/327ms14212 KiB
14Accepted3/332ms14816 KiB
15Wrong answer0/335ms15264 KiB
16Wrong answer0/339ms16036 KiB
17Accepted3/370ms17844 KiB
18Wrong answer0/372ms18308 KiB
19Accepted3/385ms19588 KiB
20Accepted3/3130ms22460 KiB
21Accepted3/3136ms22680 KiB
22Accepted3/3136ms22596 KiB