Given a tree with **N** vertices numbered **[0, n – 1]**, **K** vertices and a distance **D**, the task is to find whether there is a path from the root to some vertex such that each of the **K** vertices belongs to the path or are at most at a distance **D** from the path.

**Examples:**

Input:0 / \ / \ 1 2 / \ / \ / \ / \ 3 4 5 8 / \ / \ 6 7 / / 9 K = {6, 7, 8, 5}, D = 1Output:YESExplanation:The path( 0 - 2 - 5 - 7 )satisfies the condition. Vertices5and7are a part of the path. Vertex6is the child of vertex5and8is the child of2.Input:0 / \ / \ 1 2 / \ / \ / \ / \ 3 4 5 8 \ / \ \ / \ 10 6 7 / / 9 K = {10, 9, 8, 5}, D = 2Output:NOExplanation:No such path exists that satisfies the condition.

**Approach:**

- For every vertex, store their respective parent and depth.
- Select the deepest vertex from the given
**K**vertices - Keep replacing the
**K**vertices except the root and the deepest vertex by its parent**D**times - If the current set of
**K**vertices can form a*continuous path*, then the answer is**Yes**or**No**otherwise.

Below code is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Class to represent the Tree ` `class` `Tree { ` ` ` ` ` `int` `T; ` ` ` ` ` `// Stores the timing of traversals ` ` ` `vector<` `int` `> parent; ` ` ` ` ` `// Stores the parent of each vertex ` ` ` `vector<` `int` `> depth; ` ` ` ` ` `// Stores the depth of each vertex ` ` ` `vector<` `int` `> tin; ` ` ` ` ` `// Stores the time to reach ` ` ` `// every vertex ` ` ` `vector<` `int` `> tout; ` ` ` ` ` `// Stores the time of leaving ` ` ` `// every vertex after DFS calls ` ` ` `// from its children ` ` ` `vector<vector<` `int` `> > edges; ` ` ` ` ` `// Store the edges ` ` ` `public` `: ` ` ` `// Constructor ` ` ` `Tree(` `int` `n) ` ` ` `{ ` ` ` `T = 0; ` ` ` `parent = depth = vector<` `int` `>(n); ` ` ` `tin = tout = vector<` `int` `>(n); ` ` ` `edges = vector<vector<` `int` `> >(n); ` ` ` `} ` ` ` ` ` `// Adding edges ` ` ` `void` `addEdge(` `int` `u, ` `int` `v) ` ` ` `{ ` ` ` `edges[u].push_back(v); ` ` ` `edges[v].push_back(u); ` ` ` `} ` ` ` ` ` `void` `dfs(` `int` `v, ` `int` `p = -1, ` `int` `d = 0) ` ` ` `{ ` ` ` `// Store the time to reach vertex v ` ` ` `tin[v] = T++; ` ` ` ` ` `// Store the parent of vertex v ` ` ` `parent[v] = p; ` ` ` ` ` `// Store the depth of vertex v ` ` ` `depth[v] = d; ` ` ` ` ` `// Run DFS for all its children of v ` ` ` `for` `(` `auto` `i : edges[v]) { ` ` ` `if` `(i == p) ` ` ` `continue` `; ` ` ` ` ` `dfs(i, v, d + 1); ` ` ` `} ` ` ` ` ` `// Store the leaving time ` ` ` `// of vertex v ` ` ` `tout[v] = T++; ` ` ` `} ` ` ` ` ` `// Checks and returns whether vertex ` ` ` `// v is parent of vertex u or not ` ` ` `bool` `checkTiming(` `int` `v, ` `int` `u) ` ` ` `{ ` ` ` `if` `(tin[v] <= tin[u] ` ` ` `&& tout[u] <= tout[v]) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Checks and returns if the path exists ` ` ` `void` `pathExistence(vector<` `int` `> k, ` `int` `d) ` ` ` `{ ` ` ` `int` `deepest_vertex = k[0]; ` ` ` ` ` `// Find the deepest vertex among the ` ` ` `// given K vertices ` ` ` `for` `(` `int` `i = 0; i < k.size(); i++) { ` ` ` `if` `(depth[k[i]] > depth[deepest_vertex]) ` ` ` `deepest_vertex = k[i]; ` ` ` `} ` ` ` ` ` `// Replace each of the K vertices ` ` ` `// except for the root and the ` ` ` `// deepest vertex ` ` ` `for` `(` `int` `i = 0; i < k.size(); i++) { ` ` ` `if` `(k[i] == deepest_vertex) ` ` ` `continue` `; ` ` ` ` ` `int` `count = d; ` ` ` ` ` `while` `(count > 0) { ` ` ` ` ` `// Stop when root ` ` ` `// has been reached ` ` ` `if` `(parent[k[i]] == -1) ` ` ` `break` `; ` ` ` ` ` `k[i] = parent[k[i]]; ` ` ` `count--; ` ` ` `} ` ` ` `} ` ` ` ` ` `bool` `ans = ` `true` `; ` ` ` ` ` `// Check if each of the K-1 vertices ` ` ` `// are a parent of the deepest vertex ` ` ` `for` `(` `auto` `i : k) ` ` ` `ans &= checkTiming(i, deepest_vertex); ` ` ` ` ` `if` `(ans) ` ` ` `cout << ` `"Yes"` `<< endl; ` ` ` `else` ` ` `cout << ` `"No"` `<< endl; ` ` ` `} ` `}; ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `Tree t(11); ` ` ` ` ` `t.addEdge(0, 1); ` ` ` `t.addEdge(0, 2); ` ` ` `t.addEdge(1, 3); ` ` ` `t.addEdge(1, 4); ` ` ` `t.addEdge(2, 5); ` ` ` `t.addEdge(2, 8); ` ` ` `t.addEdge(5, 6); ` ` ` `t.addEdge(4, 10); ` ` ` `t.addEdge(3, 7); ` ` ` `t.addEdge(3, 9); ` ` ` ` ` `t.dfs(0); ` ` ` ` ` `vector<` `int` `> k = { 2, 6, 8, 5 }; ` ` ` ` ` `int` `d = 2; ` ` ` ` ` `t.pathExistence(k, d); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Find the remaining vertices of a square from two given vertices
- Make a tree with n vertices , d diameter and at most vertex degree k
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Count pairs of leaf nodes in a Binary Tree which are at most K distance apart
- Maximum difference of count of black and white vertices in a path containing vertex V
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Possible edges of a tree for given diameter, height and vertices
- Minimum Operations to make value of all vertices of the tree Zero
- Maximize the sum of products of the degrees between any two vertices of the tree
- Minimum cost to colour a Tree with no 3 adjacent vertices having same colour
- Count paths with distance equal to Manhattan distance
- Distance of chord from center when distance between center and another equal length chord is given
- Check if value exists in level-order sorted complete binary tree
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U
- Check if all the Nodes in a Binary Tree having common values are at least D distance apart
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Queries to check if the path between two nodes in a tree is a palindrome
- Check if two nodes are on same path in a tree | Set 2
- Count the Number of Binary Search Trees present in a Binary Tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.