This page is still under construction.

Parts of this page are still being built. What you see may change.

Alien Security

Interview

Time limit1sMemory limit128 MB

Summary
Given a directed graph with entry at room 0 and a target ET room, find the room closest to the target such that every path from 0 to the target passes through it, excluding the target itself.
Level

Medium6 of 10

Topics
Graph, DFS, BFS, Implementation
Solved
No attempts yet

Problem

You are in charge of security at a top-secret government research facility. Your government has captured a live extra-terrestrial (ET) and is hosting an open day for visiting researchers. Not every guest can be trusted, so each is assigned a security clearance level. Only guests with a level-5 rating may enter the room holding the ET; everyone else is free to roam the rest of the facility.

Each room is connected to others by one-way airlocks: a door can be passed through in only one direction. Every guest enters the facility through room 0.

To protect the ET you will post armed guards in exactly one room on the route to it — but not in the ET's room itself, because the guards lack the clearance to enter it. The guards inspect the identity and clearance of every guest who passes through their room, so you want to place them where they inconvenience the fewest guests who have no intention of visiting the ET. The room where the guards are posted must therefore satisfy both of the following conditions:

  1. Every guest who reaches the ET's room must first pass through the guards' room.
  2. No other room with property 1 is closer to the ET's room (and the guards may not be placed in the ET's room itself).

Determine the room in which to post the guards.

Input

The first line contains two integers RR and tt: the number of rooms and the room holding the ET. Rooms are numbered from 00 to R−1R-1, and every guest enters through room 00.

Each of the remaining lines contains two integers aa and bb, describing a one-way airlock leading from room aa to room bb (you may pass only from aa to bb). The list of doors continues until the end of the input.

Output

Print a single line:

Put guards in room N.

where NN is the room you have chosen for the guards.

Hint

The diagram below illustrates the sample facility.

Examples3

  1. Example 1

    Input
    9 4
    0 2
    2 3
    3 4
    5 3
    5 4
    3 6
    6 5
    6 7
    6 8
    4 7
    0 1
    1 7
    7 0
    
    Expected output
    Put guards in room 3.
    
  2. Example 2

    Input
    4 3
    0 1
    1 2
    2 3
    
    Expected output
    Put guards in room 2.
    
  3. Example 3

    Input
    4 3
    0 1
    0 2
    1 3
    2 3
    
    Expected output
    Put guards in room 0.