XOR Query

Hard
0/120
Average time to solve is 45m
profile
Contributed by
0 upvote
Asked in companies
ProtiumAmazonNokia

Problem statement

You are given a tree(root 0) with N vertex having N - 1 edges. You are also given an array ‘QUERY’ of size ‘Q’, where each query consists of an integer that denotes a node. You have to print the xor of all the values of nodes in the sub-tree of the given node.

Note:

Here XOR denotes the bitwise operator (^).
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer, 'T,’ denoting the number of test cases.

The first line of each test case contains two space-separated integers, ‘N’ and 'Q', denoting the number of nodes and the total queries operation to be performed.

The next ‘N’-1 lines of each test case contain two integers representing an edge between the given indices.

The next line of each test case contains ‘Q’ space-separated integers denoting the nodes of the tree on which query has to be performed.
Output Format:
For each test case, print ‘Q’ space-separated integers denoting the result of the queries.

Print the output of each test case in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
0 <= N, Q <= 10^5 
0 <= QUERY[i] < N

Time Limit: 1 sec
Sample Input 1:
1
4 3
0 1
0 2
2 3
0 2 3
Sample Output 1:
0 1 3
Explanation For Sample Output 1 :
For the first test case :
The tree for the test case will be:  

For query 1: Xor will be: 0 ^ 1 ^ 2 ^ 3 = 0
For query 2: Xor will be: 2 ^ 3 = 1
For query 3: Xor will be: 3 = 3
Sample Input 2 :
1
7 4
0 1
0 2
1 3
1 4
2 5
2 6
0 1 4 5
Sample Output 2 :
7 6 4 5
Explanation For Sample Output 2 :
For the first test case :
The tree for the test case will be:  

For query 1: Xor will be: 0 ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6= 7
For query 2: Xor will be: 1 ^ 3 ^ 4 = 6
For query 3: Xor will be: 4 = 4
For query 4: Xor will be: 5 = 5    
Hint

Try traversing the sub-tree for each query.

Approaches (2)
Brute Force

The basic idea is to traverse the subtree for each given node in the query. We traverse the nodes using the DFS and compute the XOR value of the subtree while traversing the tree.

 

Here is the algorithm :

 

  1. Create a graph (say, ‘GRAPH’) using the ‘EDGES’ array in the form of an adjacency list. (An array of lists in which ‘arr[i]’ represents the lists of vertices adjacent to the ‘ith’ vertex).
  2. Create an array (say, ‘RES’) to store the result of queries.
  3. Run a loop from 0 to ‘Q’ (say, iterator ‘i’) to traverse the queries.
    • Call the DFS function to compute the XOR of the subtree and store the value returned by the function in the ‘RES’.
  4. Return ‘RES’.

 

DFS(‘GRAPH’, ‘CURR’, ‘PREV’)  (where ‘CURR’ is the current vertex and ‘PREV’ is the previous vertex traversed).

 

  1. Create a variable (say, ‘VAL’) to store the XOR value and initialize it with ‘CURR’.
  2. Traverse the adjacent vertices of the ‘CURR’ vertex (say, ‘V’)
    • Check if ‘V’ is not equal to ‘PREV’
      • Recursively call the DFS function to traverse the tree and update the ‘VAL’ by taking the XOR of it by the value returned by the function.
  3. Return ‘VAL’.
Time Complexity

O(N * Q), where ‘N’ is the number of vertices and ‘Q’ is the number of queries.

 

We run a loop to answer all the queries which take O(Q) time and in the worst case for each query we traverse all the nodes which take O(N) time. Therefore, the overall time complexity will be O(N * Q).

Space Complexity

O(N), where ‘N’ is the number of vertices.
 

For each vertex, we store its adjacent vertices; therefore, storing them in the form of a graph will require O(N) space. The recursive stack of the HELPER function will contain all the neighbors of a vertex, which at most can be ‘N’ vertices. Therefore, the overall space complexity will be O(N).

Code Solution
(100% EXP penalty)
XOR Query
Full screen
Console