A relay network has n towers, numbered 1 to n, and m two-way wires. A message starts at a source tower s and can travel through at most k wires total (each wire crossed counts as one hop).
Count how many DISTINCT towers can receive the message, counting the source tower itself (which receives it with zero hops).
Line 1: two integers n and m — number of towers (1-indexed) and number of wires.
Next m lines: two integers u v — an undirected wire between tower u and tower v.
Last line: two integers s k — the source tower and the maximum number of hops allowed.
A single integer: the number of towers reachable from s within k hops (inclusive).
Example 1
Input
5 4 1 2 2 3 3 4 4 5 1 2
Expected
3
Explanation
From tower 1 within 2 hops: tower 1 (0 hops), tower 2 (1 hop), tower 3 (2 hops). Count 3.
Example 2
Input
3 2 1 2 2 3 1 0
Expected
1
Explanation
With k=0, only the source tower itself counts. Output 1.
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 →