Xor tree

4.5

8 votes
Graphs, Algorithms, Depth First Search, C++
Problem

You are given an undirected tree with N nodes. Every node is initially assigned a value equal to 0.

Perform Q queries that are given on the tree as follows:

  • u v w: For all the nodes present on a simple path between nodes u and v take the xor of node value with w

Task

Find the sum of values assigned to all the nodes after performing Q queries.

Input

  • First line contains an integer T, denoting the number of test cases.
  • For each test case:
    • The first line contains two space separated integer N, Q.
    • The next N - 1 lines contain two space-separated integers u, v denoting an edge between node u and node v.
    • The next Q lines contains three space-separated integers denoting the query.

Output 

For each test case in a new line, print the sum of values assigned to each node after performing Q queries.

Constraints

1T51N,Q1050w109

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Initial values of the nodes in the tree is [0,0,0].

For 1st Query:

  • The simple paths between node 2 and 3 consists of nodes [2,1,3].
  • Value of the nodes will be [5,5,5].

For 2nd Query:

  • The simple paths between node 3 and 3 consists of nodes [3].
  • Value of the nodes will be [5,5,0].

Sum of all the nodes in the tree is 5+5=10

Editor Image

?