41622023-03-15 17:56:50kristofElágazás nélküli úton levő települések (50 pont)python3Runtime error 19/50137ms23284 KiB
from collections import defaultdict

tole = 0
elozo = 0

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 = []
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/021ms11940 KiB
2Accepted0/0137ms20980 KiB
3Runtime error0/220ms12376 KiB
4Wrong answer0/220ms12672 KiB
5Accepted2/219ms12672 KiB
6Runtime error0/219ms12888 KiB
7Wrong answer0/220ms13156 KiB
8Accepted2/229ms13992 KiB
9Wrong answer0/239ms14972 KiB
10Accepted2/250ms16444 KiB
11Accepted2/281ms17956 KiB
12Accepted2/276ms18372 KiB
13Wrong answer0/328ms14788 KiB
14Runtime error0/335ms17424 KiB
15Runtime error0/335ms15988 KiB
16Runtime error0/337ms16472 KiB
17Runtime error0/370ms18704 KiB
18Runtime error0/371ms19036 KiB
19Wrong answer0/385ms20300 KiB
20Accepted3/3131ms23052 KiB
21Accepted3/3135ms23168 KiB
22Accepted3/3134ms23284 KiB