41732023-03-15 19:13:53kristofElágazás nélküli úton levő települések (50 pont)python3Wrong answer 10/50144ms23376 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])


print(len(kiElerheto))
print(*(kiElerheto))
SubtaskSumTestVerdictTimeMemory
base10/50
1Accepted0/020ms12036 KiB
2Wrong answer0/0144ms21096 KiB
3Wrong answer0/220ms12508 KiB
4Accepted2/220ms12648 KiB
5Accepted2/220ms12792 KiB
6Accepted2/219ms12876 KiB
7Accepted2/219ms12812 KiB
8Wrong answer0/229ms13720 KiB
9Wrong answer0/237ms14624 KiB
10Wrong answer0/250ms15804 KiB
11Accepted2/278ms17992 KiB
12Wrong answer0/278ms17996 KiB
13Wrong answer0/327ms14496 KiB
14Wrong answer0/332ms15276 KiB
15Wrong answer0/335ms16056 KiB
16Wrong answer0/339ms16808 KiB
17Wrong answer0/368ms18900 KiB
18Wrong answer0/375ms19248 KiB
19Wrong answer0/385ms20260 KiB
20Wrong answer0/3138ms23172 KiB
21Wrong answer0/3136ms23332 KiB
22Wrong answer0/3136ms23376 KiB