86502024-01-24 23:23:56KezdőKerékpártúra (50 pont)python3Accepted 50/50342ms51024 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):
        k,v = [int(i) for i in stdin.readline().split()]
        oda[k].append(v)
        vissza[v].append(k)

    sor = [K]
    volt_vissza = [False]*(N+1)
    volt_vissza[K] = True
    while sor != []:
        P = sor.pop(0)
        for x in vissza[P]:
            if not volt_vissza[x]:
                volt_vissza[x] = True
                sor.append(x)

    sor = [K]
    volt = [False]*(N+1)
    volt[K] = True
    tura = [False]*(N+1)
    for i in oda[K]:
        tura[i] = True
    while sor != []:
        P = sor.pop(0)
        for x in oda[P]:
            if not volt[x]:
                volt[x] = True
                sor.append(x)
                if volt_vissza[x]:
                    for y in oda[x]:
                        tura[y] = True

    tura[K] = False
    t = [i for i in range(1,N+1) if tura[i]]
    print(len(t))
    print(*t)

main()
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/017ms11500 KiB
2Accepted0/065ms19772 KiB
3Accepted2/217ms12044 KiB
4Accepted2/219ms11996 KiB
5Accepted2/217ms12480 KiB
6Accepted2/218ms12888 KiB
7Accepted2/218ms13028 KiB
8Accepted2/223ms13540 KiB
9Accepted2/223ms13892 KiB
10Accepted2/225ms13776 KiB
11Accepted2/228ms14532 KiB
12Accepted2/246ms16752 KiB
13Accepted2/243ms16648 KiB
14Accepted2/268ms19236 KiB
15Accepted3/397ms25460 KiB
16Accepted4/4103ms26720 KiB
17Accepted4/4136ms30632 KiB
18Accepted3/3123ms29292 KiB
19Accepted3/3109ms27712 KiB
20Accepted3/3289ms46896 KiB
21Accepted3/3342ms50136 KiB
22Accepted3/3331ms51024 KiB