41612023-03-15 17:54:07kristofElágazás nélküli úton levő települések (50 pont)python3Runtime error 19/50138ms24048 KiB
from collections import defaultdict

tole = 0
elozo = ""

ki = []

def szomszed(froma):
    global tole
    global elozo
    if (len(graf[froma])) < 3:
        #print(froma)
        tole += 1
        if graf[froma][0] == elozo:
            ki.append(froma)
            elozo = froma
            return szomszed(graf[froma][1])
        else:
            return szomszed(graf[froma][0])
    ki.append(froma)
    return 0

n, m = list(map(int, input().split()))
graf = defaultdict(list)
arr = [0 for i in range(n)]

for i in range(m):
    honnan, hova = list(map(int, input().split()))
    graf[honnan].append(hova)
    graf[hova].append(honnan)
    arr[honnan-1] += 1
    arr[hova-1] += 1

zsakfalvak = [i+1 for i, value in enumerate(arr) if value == 1]

asd = 0
if len(zsakfalvak) > 0:

    for i in zsakfalvak:
        elozo = i
        szomszed(i)
        asd += tole
        tole = 0

print(asd)
print(*sorted(ki))
SubtaskSumTestVerdictTimeMemory
base19/50
1Accepted0/020ms11944 KiB
2Accepted0/0136ms21072 KiB
3Runtime error0/219ms12704 KiB
4Wrong answer0/220ms12952 KiB
5Accepted2/219ms13200 KiB
6Runtime error0/220ms13328 KiB
7Wrong answer0/219ms13372 KiB
8Accepted2/229ms14368 KiB
9Wrong answer0/237ms15348 KiB
10Accepted2/248ms16256 KiB
11Accepted2/275ms18224 KiB
12Accepted2/278ms18300 KiB
13Wrong answer0/327ms14644 KiB
14Runtime error0/335ms17264 KiB
15Runtime error0/335ms16524 KiB
16Runtime error0/337ms16900 KiB
17Runtime error0/368ms19180 KiB
18Runtime error0/372ms19568 KiB
19Wrong answer0/386ms20644 KiB
20Accepted3/3133ms23720 KiB
21Accepted3/3134ms23972 KiB
22Accepted3/3138ms24048 KiB