Count nodes having Bitwise XOR of all edges in their path from the root equal to K
Given a Binary Tree consisting of N nodes and two integers R and K. Each edge of the tree has a positive integer associated with it, given in the form {u, v, w} where the edge (u, v) has weight w. The task is to calculate the number of nodes S having Bitwise XOR of all edges in the path from root R to S is equal to K.
Examples:
Input: R = 1, K = 0, N = 7, Edges[][] = {{1, 2, 3}, {1, 3, 1}, {2, 4, 3}, {2, 5, 4}, {3, 6, 1}, {3, 7, 2}}
Output: 2
Explanation:
Representation of the given Binary Tree:
The following pair of nodes have a Bitwise XOR of edges in the path connecting them as K = 0:
Pair 1: (1, 4) = (3 ^ 3) = 0
Pair 2: (1, 6) = (1 ^ 1) = 0
Input: R = 1, K = 0, N = 9, Edges[][] = {{1, 2, 3}, {1, 3, 2}, {2, 4, 3}, {2, 5, 4}, {3, 6, 1}, {3, 7, 2}, {6, 8, 3}, {6, 9, 7}}
Output: 3
Explanation:
The representation of given Binary Tree is as follows:
The following pair of nodes have a Bitwise XOR of edges in the path connecting them as K = 0:
Pair 1: (1, 4) = (3 ^ 3) = 0
Pair 2: (1, 8) = (2 ^ 1 ^ 3) = 0
Pair 3: (1, 7) = (2 ^ 2) = 0
Approach: The problem can be solved using the Depth First Search approach. Follow the steps below to solve the problem:
- Initialize the variable ans and xor with 0 to store the number of pairs and the current xor of edges.
- Traverse the given tree using Depth First Search starting from the given root vertex R.
- For every node u, visit its adjacent nodes.
- For each edge {u, v}, if xor is equal to K, increment ans by 1. Otherwise, for the current edge {u, v, w}, update xor as xor = (xor^w) where ^ is the bitwise XOR.
- After traversing, print the value stored in the counter ans as the number of pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<pair< int , int > > adj[100005];
int visited[100005] = { 0 };
int ans = 0;
void dfs( int node, int xorr, int k)
{
visited[node] = 1;
if (node != 1 && xorr == k)
ans++;
for ( auto x : adj[node]) {
if (!visited[x.first]) {
int xorr1 = xorr ^ x.second;
dfs(x.first, xorr1, k);
}
}
}
void countNodes( int N, int K, int R,
vector<vector< int > > edges)
{
for ( int i = 0; i < N - 1; i++) {
int u = edges[i][0], v = edges[i][1],
w = edges[i][2];
adj[u].push_back({ v, w });
adj[v].push_back({ u, w });
}
dfs(R, 0, K);
cout << ans << "\n" ;
}
int main()
{
int K = 0, R = 1;
vector<vector< int > > edges
= { { 1, 2, 3 }, { 1, 3, 1 },
{ 2, 4, 3 }, { 2, 5, 4 },
{ 3, 6, 1 }, { 3, 7, 2 } };
int N = edges.size();
countNodes(N, K, R, edges);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair( int first,
int second)
{
this .first = first;
this .second = second;
}
}
static Vector<pair> []adj =
new Vector[ 100005 ];
static int visited[] =
new int [ 100005 ];
static int ans = 0 ;
static void dfs( int node,
int xorr,
int k)
{
visited[node] = 1 ;
if (node != 1 &&
xorr == k)
ans++;
for (pair x : adj[node])
{
if (visited[x.first] != 1 )
{
int xorr1 = xorr ^ x.second;
dfs(x.first, xorr1, k);
}
}
}
static void countNodes( int N, int K,
int R, int [][] edges)
{
for ( int i = 0 ; i < N - 1 ; i++)
{
int u = edges[i][ 0 ],
v = edges[i][ 1 ],
w = edges[i][ 2 ];
adj[u].add( new pair(v, w ));
adj[v].add( new pair(u, w ));
}
dfs(R, 0 , K);
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int K = 0 , R = 1 ;
for ( int i = 0 ; i < adj.length; i++)
adj[i] = new Vector<pair>();
int [][] edges = {{ 1 , 2 , 3 },
{ 1 , 3 , 1 },
{ 2 , 4 , 3 },
{ 2 , 5 , 4 },
{ 3 , 6 , 1 },
{ 3 , 7 , 2 }};
int N = edges.length;
countNodes(N, K, R, edges);
}
}
|
Python3
adj = [[] for i in range ( 100005 )]
visited = [ 0 ] * 100005
ans = 0
def dfs(node, xorr, k):
global ans
visited[node] = 1
if (node ! = 1 and xorr = = k):
ans + = 1
for x in adj[node]:
if ( not visited[x[ 0 ]]):
xorr1 = xorr ^ x[ 1 ]
dfs(x[ 0 ], xorr1, k)
def countNodes(N, K, R, edges):
for i in range (N - 1 ):
u = edges[i][ 0 ]
v = edges[i][ 1 ]
w = edges[i][ 2 ]
adj[u].append([v, w])
adj[v].append([u, w])
dfs(R, 0 , K)
print (ans)
if __name__ = = '__main__' :
K = 0
R = 1
edges = [ [ 1 , 2 , 3 ],[ 1 , 3 , 1 ],
[ 2 , 4 , 3 ],[ 2 , 5 , 4 ],
[ 3 , 6 , 1 ],[ 3 , 7 , 2 ] ]
N = len (edges)
countNodes(N, K, R, edges)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class pair
{
public int first,
second;
public pair( int first,
int second)
{
this .first = first;
this .second = second;
}
}
static List<pair> []adj =
new List<pair>[100005];
static int []visited =
new int [100005];
static int ans = 0;
static void dfs( int node,
int xorr,
int k)
{
visited[node] = 1;
if (node != 1 &&
xorr == k)
ans++;
foreach (pair x in adj[node])
{
if (visited[x.first] != 1)
{
int xorr1 = xorr ^ x.second;
dfs(x.first, xorr1, k);
}
}
}
static void countNodes( int N, int K,
int R, int [,] edges)
{
for ( int i = 0; i < N - 1; i++)
{
int u = edges[i,0];
int v = edges[i,1],
w = edges[i,2];
adj[u].Add( new pair(v, w ));
adj[v].Add( new pair(u, w ));
}
dfs(R, 0, K);
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int K = 0, R = 1;
for ( int i = 0; i < adj.Length; i++)
adj[i] = new List<pair>();
int [,] edges = {{1, 2, 3},
{1, 3, 1},
{2, 4, 3},
{2, 5, 4},
{3, 6, 1},
{3, 7, 2}};
int N = edges.GetLength(0);
countNodes(N, K, R, edges);
}
}
|
Javascript
<script>
let adj = [];
for (let i = 0; i < 100005; i++) {
adj.push([])
}
let visited = new Array(100005).fill(0);
let ans = 0;
function dfs(node, xorr, k) {
visited[node] = 1;
if (node != 1 && xorr == k)
ans++;
for (let x of adj[node]) {
if (!visited[x[0]]) {
let xorr1 = xorr ^ x[1];
dfs(x[0], xorr1, k);
}
}
}
function countNodes(N, K, R, edges) {
for (let i = 0; i < N - 1; i++) {
let u = edges[i][0], v = edges[i][1],
w = edges[i][2];
adj[u].push([v, w]);
adj[v].push([u, w]);
}
dfs(R, 0, K);
document.write(ans + "<br>" );
}
let K = 0, R = 1;
let edges
= [[1, 2, 3], [1, 3, 1],
[2, 4, 3], [2, 5, 4],
[3, 6, 1], [3, 7, 2]];
let N = edges.length;
countNodes(N, K, R, edges);
</script>
|
Time Complexity: O(N) where N is the number of nodes.
Auxiliary Space: O(N)
Last Updated :
18 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...