89462024-02-07 15:14:30petergalambosKerékpártúra (50 pont)python3Accepted 50/50358ms51088 KiB
from sys import stdin, stdout
def main():
    N, M, K = [int(i) for i in input().split()]
    oda = [[] for i in range(N+1)]
    vissza = [[] for i in range(N+1)]
    for i in range(M):
        x,y = [int(i) for i in stdin.readline().split()]
        oda[x].append(y)
        vissza[y].append(x)

    sor = [K]
    vvolt = [False] * (N+1)
    vvolt[K] = True
    while sor != []:
        P = sor.pop(0)
        for v in vissza[P]:
            if not vvolt[v]:
                vvolt[v] = True
                sor.append(v)       
    S = set()
    for o in oda[K]:
        S.add(o)
        
    sor = [K]
    volt = [False] * (N+1)
    volt[K] = True
    while sor != []:
        P = sor.pop(0)
        for o in oda[P]:
            if not volt[o]:
                volt[o] = True
                sor.append(o)
                if vvolt[o]:
                    for od in oda[o]:
                        S.add(od)
    S.remove(K)
    print(len(S))
    print(*S)
    
main()
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/018ms11492 KiB
2Accepted0/063ms19696 KiB
3Accepted2/218ms12092 KiB
4Accepted2/217ms11848 KiB
5Accepted2/217ms12392 KiB
6Accepted2/217ms12496 KiB
7Accepted2/217ms12544 KiB
8Accepted2/224ms13116 KiB
9Accepted2/224ms13336 KiB
10Accepted2/225ms13380 KiB
11Accepted2/228ms14436 KiB
12Accepted2/246ms16152 KiB
13Accepted2/246ms16248 KiB
14Accepted2/270ms18368 KiB
15Accepted3/397ms24636 KiB
16Accepted4/4107ms26088 KiB
17Accepted4/4140ms30060 KiB
18Accepted3/3125ms28692 KiB
19Accepted3/3112ms27460 KiB
20Accepted3/3317ms46912 KiB
21Accepted3/3351ms49716 KiB
22Accepted3/3358ms51088 KiB