Count Nodes within K-Distance Problem Statement

Given a connected, undirected, and acyclic graph where some nodes are marked and a positive integer 'K'. Your task is to return the count of nodes such that the distance from each marked node is less than 'K'.

Explanation:

A connected graph means there is a path between every pair of vertices. Acyclic means the graph does not contain any cycles, and undirected means edges are bidirectional.

Input:

The first line contains two space-separated integers 'V' and 'E', representing the number of vertices and edges.
The next 'E' lines contain two space-separated integers denoting the vertices connected by an edge.
The following line contains an integer 'K'.
The subsequent line contains an integer 'M', the number of marked nodes.
Finally, the line contains 'M' space-separated integers denoting the marked nodes.

Output:

Output a single integer for each test case, representing the count of nodes with distances less than 'K' from all marked nodes.

Example:

Graph Example

Marked nodes are circled in red.
In the graph, nodes 1, 2, and 4 are marked. With K as 3, nodes 5, 9, 8, 2, 0, and 7 have distances less than 3 from all marked nodes. Hence, the total count is 6.

Constraints:

  • 1 <= 'V' <= 104
  • 0 <= 'E' <= 104
  • 1 <= |'V'| <= 'V'
  • 1 <= 'K' <= 35000
  • 1 <= 'M' <= 7000

Note:

No need to output, just implement the function as required.
Be the first one to answer
Add answer anonymously...
Comviva Technology Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter