74072024-01-08 14:59:16petergalambosElágazás nélküli úton levő települések (50 pont)python3Accepted 50/5072ms21356 KiB
from sys import stdin
def main():
    N, M = [int(i) for i in input().split()]

    K = [[] for i in range(N+1)]

    for i in range(M): #kapcsolatok boelvasása
        x,y = [int(i) for i in stdin.readline().split()]
        K[x].append(y)
        K[y].append(x)
        
    Z = [i for i,s in enumerate(K) if len(s)==1] #zsákfalvak kiválasztása

    S = []
    for z in Z:
        R = z
        P = K[z][:]
        while len(P) == 1:
            P = P[0]
            if P not in S:
                S.append(P)
            i = P
            P = K[P][:]
            P.remove(R)
            R = i
            
    print(len(S))
    S.sort()
    print(*S)
main()
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/017ms11296 KiB
2Accepted0/072ms19892 KiB
3Accepted2/217ms11752 KiB
4Accepted2/217ms11952 KiB
5Accepted2/217ms11856 KiB
6Accepted2/217ms12212 KiB
7Accepted2/217ms12368 KiB
8Accepted2/221ms13032 KiB
9Accepted2/226ms13816 KiB
10Accepted2/232ms14844 KiB
11Accepted2/245ms16700 KiB
12Accepted2/245ms16700 KiB
13Accepted3/319ms13052 KiB
14Accepted3/325ms14048 KiB
15Accepted3/328ms14356 KiB
16Accepted3/345ms15244 KiB
17Accepted3/343ms16952 KiB
18Accepted3/346ms17200 KiB
19Accepted3/350ms18304 KiB
20Accepted3/371ms21256 KiB
21Accepted3/372ms21356 KiB
22Accepted3/372ms21176 KiB