Find the minimum difference between pairs in a simple path of tree C++
C++
// C++ implementation
#include <bits/stdc++.h>
using namespace std;
// An utility function to add an edge in an
// undirected graph.
void addEdge(vector<int> v[],
int x,
int y)
{
v[x].push_back(y);
v[y].push_back(x);
}
// A function to print the path between
// the given pair of nodes.
void printPath(vector<int> stack)
{
int i;
for (i = 0; i < (int)stack.size() - 1;
i++) {
cout << stack[i] << " -> ";
}
cout << stack[i];
}
// An utility function to do
// DFS of graph recursively
// from a given vertex x.
void DFS(vector<int> v[],
bool vis[],
int x,
int y,
vector<int> stack)
{
stack.push_back(x);
if (x == y) {
// print the path and return on
// reaching the destination node
printPath(stack);
return;
}
vis[x] = true;
// A flag variable to keep track
// if backtracking is taking place
int flag = 0;
if (!v[x].empty()) {
for (int j = 0; j < v[x].size(); j++) {
// if the node is not visited
if (vis[v[x][j]] == false) {
DFS(v, vis, v[x][j], y, stack);
flag = 1;
}
}
}
if (flag == 0) {
// If backtracking is taking
// place then pop
stack.pop_back();
}
}
// A utility function to initialise
// visited for the node and call
// DFS function for a given vertex x.
void DFSCall(int x,
int y,
vector<int> v[],
int n,
vector<int> stack)
{
// visited array
bool vis[n + 1];
memset(vis, false, sizeof(vis));
// DFS function call
DFS(v, vis, x, y, stack);
}
// Driver Code
int main()
{
int n = 10;
vector<int> v[n], stack;
// Vertex numbers should be from 1 to 9.
addEdge(v, 1, 2);
addEdge(v, 1, 3);
addEdge(v, 2, 4);
addEdge(v, 2, 5);
addEdge(v, 2, 6);
addEdge(v, 3, 7);
addEdge(v, 3, 8);
addEdge(v, 3, 9);
// Function Call
DFSCall(4, 8, v, n, stack);
return 0;
}
Also in C++:
- Title
- how to find absolute value in c++
- Category
- C++
- Title
- dijkstra in c++
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- what is difffrence between s.length() and s.size()
- Category
- C++
- Title
- passing a vector to a function c++
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- cpp loop through object
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- how to read and write in a file c++
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- remove element by index from vector c++
- Category
- C++
- Title
- c++ passing two dimensional array to function
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- knapsack
- Category
- C++
- Title
- C++ int to char*
- Category
- C++
- Title
- memset c++
- Category
- C++
- Title
- c++ program how to let the user choose different game modes
- Category
- C++
- Title
- how to find the number of cycles in a graph C++
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- c++ tutorial
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- c++ replace n substrings
- Category
- C++
- Title
- std::reverse
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- maximum subarray sum in c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- Given an undirected graph, count the number of connected components.
- Category
- C++
- Title
- c++ looping through a vector
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- c++ typedef
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- unordered_map c++
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- c++ calculator program using switch case
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- tuple c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- sfml base program
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- vector stl c++
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- Html tab
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- container class in c++
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- multiple words C++ in same
- Category
- C++
- Title
- FInd the element which appears more than n/2 times C++
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- length of string c++
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- shuffle vector c++
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- sort a pair using c++ stl
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++