Beauty of a tree having m edges and weights as w1,w2......wm is defined as w1⊕w2⊕w3...⊕wn−1⊕wn. The beauty of a tree having a single vertex is 0
You are given a weighted tree having n vertices. Recall that a tree is a connected undirected graph without cycles.
You are then given n edges where the ith edge has wi weight and connects the two vertices ai and bi
You are also given q queries. In every query you will be given a number u which denotes the uth edge of the tree. For every query your task is to find the absolute difference between the beauty of the two components created if that edge had been removed from the tree.
it is garaunteed that each vertex is a part of atleast one edge.
See the sample test case for further explaination
The first line of the input contains two integers n(2≤n≤2⋅105) and q(1≤q≤2⋅105) — the number of vertices in the tree and the number of queries respectively.
The following n−1 lines each contain three space-separated integers, where the ith line containing integers ai(1≤ai≤n) , bi(1≤bi≤n), wi(1≤wi<1012)denotes an edge from ai to bi having weight wi.
The next q lines contains a single integer u(1≤u<n) denoting the uth edge that is considered to be removed for that query.
For every query print the absolute difference between the beauty of the created components on a separate line
The graph for the first test case is shown below.
First Query
After removing the 1st edge connecting vertices 1 and 2 we get the below two components.
One component consists of edges having weight 9, 12, 4. Its beauty is calculated as 9 xor 12 xor 4 which equals 1. The other component consists of edges having weights as 8, 2. Its beauty is calculated as 8 xor 2 which equals to 10.
Therefore the absolute difference between the beauty of the two components is 10-1=9.
Second Query
After removing the 5th edge connecting vertices 1 and 7 we get the below two components.
One component consists of edges having weight 4, 6, 8 2. Its beauty is calculated as 4 xor 6 xor 8 xor 2 which equals 8. The other component consists of a single edge having weight 9. Its beauty is 9.
Therefore the absolute difference between the beauty of the two components is 9-8=1.