A relay network has n nodes numbered 1 through n, joined by bidirectional links. A broadcast starts at a source node s and travels across links; reaching a neighbor costs one hop. A node's hop-distance from s is the minimum number of links on any path from s to it.
Given the source s and a hop budget K, count how many nodes other than s have hop-distance at most K from s. Nodes with no path from s are never counted.
Line 1: two integers n and m.
Next m lines: two integers u and v, a bidirectional link between nodes u and v.
Last line: two integers s and K.
A single integer: the number of nodes (excluding s) reachable within K hops.
Example 1
Input
6 5 1 2 1 3 2 4 3 5 5 6 1 2
Expected
4
Explanation
From node 1 the hop-distances are 2:1, 3:1, 4:2, 5:2, 6:3. Within 2 hops the reachable others are {2,3,4,5}: 4 nodes.
Example 2
Input
6 5 1 2 1 3 2 4 3 5 5 6 1 1
Expected
2
Explanation
With a budget of 1 hop only nodes 2 and 3 are within reach: 2 nodes.
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 →