# Find the number of distinct pairs of vertices which have a distance of exactly k in a tree

Given an integer **k** and a tree with **n** nodes. The task is to count the number of distinct pairs of vertices which have a distance of exactly **k**.

**Examples:**

Input:k = 2

Output:4

Input:k = 3

Output:2

**Approach:** This problem can be solved using dynamic programming. For every vertex v of the tree, we calculate values d[v][lev] (0 <= lev <= k). This value indicates the number of vertices having distance lev from v. Note that d[v][0] = 0.

Then we calculate the answer. For any vertex v number of pairs will be a product of the number of vertices at level j – 1 and level k – j.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define N 5005 ` ` ` `// To store vertices and value of k ` `int` `n, k; ` ` ` `vector<` `int` `> gr[N]; ` ` ` `// To store number vertices at a level i ` `int` `d[N][505]; ` ` ` `// To store the final answer ` `int` `ans = 0; ` ` ` `// Function to add an edge between two nodes ` `void` `Add_edge(` `int` `x, ` `int` `y) ` `{ ` ` ` `gr[x].push_back(y); ` ` ` `gr[y].push_back(x); ` `} ` ` ` `// Function to find the number of distinct ` `// pairs of the vertices which have a distance ` `// of exactly k in a tree ` `void` `dfs(` `int` `v, ` `int` `par) ` `{ ` ` ` `// At level zero vertex itself is counted ` ` ` `d[v][0] = 1; ` ` ` `for` `(` `auto` `i : gr[v]) { ` ` ` `if` `(i != par) { ` ` ` `dfs(i, v); ` ` ` ` ` `// Count the pair of vertices at ` ` ` `// distance k ` ` ` `for` `(` `int` `j = 1; j <= k; j++) ` ` ` `ans += d[i][j - 1] * d[v][k - j]; ` ` ` ` ` `// For all levels count vertices ` ` ` `for` `(` `int` `j = 1; j <= k; j++) ` ` ` `d[v][j] += d[i][j - 1]; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `n = 5, k = 2; ` ` ` ` ` `// Add edges ` ` ` `Add_edge(1, 2); ` ` ` `Add_edge(2, 3); ` ` ` `Add_edge(3, 4); ` ` ` `Add_edge(2, 5); ` ` ` ` ` `// Function call ` ` ` `dfs(1, 0); ` ` ` ` ` `// Required answer ` ` ` `cout << ans; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

4

## Recommended Posts:

- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Find distance between two nodes of a Binary Tree
- Queries to find distance between two nodes of a Binary tree
- Find distance from root to given node in a binary tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Querying the number of distinct colors in a subtree of a colored tree using BIT
- 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
- Queries to find whether a number has exactly four distinct factors or not
- Make a tree with n vertices , d diameter and at most vertex degree k
- Pairs with same Manhattan and Euclidean distance
- Given GCD G and LCM L, find number of possible pairs (a, b)
- All possible co-prime distinct element pairs within a range [L, R]
- Find number of magical pairs of string of length L

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.