Count nodes having Bitwise XOR of all edges in their path from the root equal to K | Initialize the adjacency list to represent the tree ; Marks visited / unvisited vertices ; Stores the required count of nodes ; DFS to visit each vertex ; Mark the current node as visited ; Update the counter xor is K ; Visit adjacent nodes ; Calculate Bitwise XOR of edges in the path ; Recursive call to dfs function ; Function to construct the tree and prrequired count of nodes ; Add edges ; Print answer ; Driver Code ; Given K and R ; Given edges ; Number of vertices ; Function call
adj = [ [ ] for i in range ( 100005 ) ] NEW_LINE visited = [ 0 ] * 100005 NEW_LINE ans = 0 NEW_LINE def dfs ( node , xorr , k ) : NEW_LINE INDENT global ans NEW_LINE visited [ node ] = 1 NEW_LINE if ( node != 1 and xorr == k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT for x in adj [ node ] : NEW_LINE INDENT if ( not visited [ x [ 0 ] ] ) : NEW_LINE INDENT xorr1 = xorr ^ x [ 1 ] NEW_LINE dfs ( x [ 0 ] , xorr1 , k ) NEW_LINE DEDENT DEDENT DEDENT def countNodes ( N , K , R , edges ) : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] NEW_LINE v = edges [ i ] [ 1 ] NEW_LINE w = edges [ i ] [ 2 ] NEW_LINE adj [ u ] . append ( [ v , w ] ) NEW_LINE adj [ v ] . append ( [ u , w ] ) NEW_LINE DEDENT dfs ( R , 0 , K ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 0 NEW_LINE R = 1 NEW_LINE edges = [ [ 1 , 2 , 3 ] , [ 1 , 3 , 1 ] , [ 2 , 4 , 3 ] , [ 2 , 5 , 4 ] , [ 3 , 6 , 1 ] , [ 3 , 7 , 2 ] ] NEW_LINE N = len ( edges ) NEW_LINE countNodes ( N , K , R , edges ) NEW_LINE DEDENT