Embercode
✕
GraphsCoding problems

Graphs: coding problems

Write a program that reads the input and prints the answer. Run checks it against every test, including hidden ones.

0 of 7 solved

  • A building has n rooms numbered 0 to n-1. Some doors only open one way. A robot starts in room s. It can walk through any door in the direction the door opens.

    How many rooms can the robot reach? Count room s itself.

    Read the rooms and doors, then print that count.

    Input. The first line holds n, m and s: the number of rooms (1 to 100000), the number of one-way doors (0 to 200000) and the start room. Each of the next m lines holds two integers a and b: a door from room a to room b.

    Output. One integer: how many rooms the robot can reach from s, including s.

    Example 1 input

    5 4 0
    0 1
    1 2
    3 4
    2 0
    

    Output

    3
    

    Example 2 input

    4 0 2
    

    Output

    1