A company has n offices, numbered 1 to n, connected by a one-way phone tree: m directed links, where a link u v means office u can call office v directly (but not necessarily the reverse). An emergency alert starts at headquarters, office s, and propagates along the directed links: any office that receives the alert immediately relays it to every office it has a direct link to.
Report how many of the n offices NEVER receive the alert (office s itself always receives it, so it never counts as unreached).
Line 1: two integers n and m — number of offices (1-indexed) and number of links.
Next m lines: two integers u v — a directed link from office u to office v.
Last line: one integer s — the headquarters office.
A single integer: the number of offices that never receive the alert.
Example 1
Input
4 2 1 2 2 3 1
Expected
1
Explanation
From office 1, the alert reaches 1, 2, and 3 but never 4 (no incoming link to 4). 1 office unreached.
Example 2
Input
3 2 2 1 3 1 1
Expected
2
Explanation
Both links point INTO office 1, so from 1 the alert cannot reach 2 or 3. 2 offices unreached.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →