Find if path length is even or odd between given Tree nodes for Q queries
Given a generic tree consisting of N nodes and (N – 1) edges and an array of queries query[] of size Q consisting of the type {A, B}, the task for each query is to check whether the path length between two given nodes A and B is even or odd.
Examples:
Input: query[] = {{2, 4}, {4, 0}}
Output:
Odd
Even
Explanation:
For the 1st query A = 2 and B = 4. The path from A to B is 2 -> 0 -> 1 -> 3 -> 4 having a length of 5 i.e., Odd.
For the 2nd query A = 4 and B = 0. The path from A to B is 4 -> 3 -> 1 -> 0 having a length of 4 i.e., Even.
Approach: The given problem can be efficiently solved by converting the tree into a Bipartite Graph. It can be observed that if the given nodes A and B in a query are on the same side in the constructed bipartite graph, then the path length between A and B must be odd and if A and B are on different sides, then the path length must be odd. Below are the steps to follow:
- Traverse the given tree using the BFS Traversal.
- Divide all nodes into 2 sets such that all the two adjacent nodes in the tree are in different sets (i.e., 0 or 1). In order to do so, assing an alternating set number to each level during the BFS traversal by assigning the set number of current nodes = 1 XOR the number of the parent of the current node.
- After completing the above steps, traverse the given array of queries query[] and if the set number of both the nodes are the same, the path length of A to B is Odd. Otherwise, it is Even.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Stores the input treevector<vector<int> > adj(100000); // Stores the set number of all nodesvector<int> setNum(100000); // Function to add an edge in the treevoid addEdge(int a1, int a2){ adj[a1].push_back(a2); adj[a2].push_back(a1);} // Function to convert the given tree// into a bipartite graph using BFSvoid toBipartite(int N){ // Set the set number to -1 for // all node of the given tree setNum.assign(N, -1); // Stores the current node during // the BFS traversal of the tree queue<int> q; // Initialize the set number of // 1st node and enqueue it q.push(0); setNum[0] = 0; // BFS traversal of the given tree while (!q.empty()) { // Current node int v = q.front(); q.pop(); // Traverse over all neighbours // of the current node for (int u : adj[v]) { // If the set is not assigned if (setNum[u] == -1) { // Assign set number to node u setNum[u] = setNum[v] ^ 1; q.push(u); } } }} // Function to find if the path length// between node A and B is even or oddvoid pathLengthQuery(int A, int B){ // If the set number of both nodes is // same, path length is odd else even if (setNum[A] == setNum[B]) { cout << "Odd" << endl; } else { cout << "Even" << endl; }} // Driver Codeint main(){ int N = 7; addEdge(0, 1); addEdge(0, 2); addEdge(1, 3); addEdge(3, 4); addEdge(3, 5); addEdge(2, 6); // Function to convert tree into // bipartite toBipartite(N); pathLengthQuery(4, 2); pathLengthQuery(0, 4); return 0;} |
Odd Even
Time Complexity: O(N + Q)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.



