XOR Tree Codechef solutions Today | Codechef Starters 72 ✅ (100/100) FULL | AC Code ✅| GGTREE
Problem
There is a tree with vertices, rooted at vertex . Vertex has the value written on it.
Alice walks along this tree, starting at the root.
When she is at vertex :
- If has no children, she stops.
- Otherwise, suppose has children. She picks one of them at random (each one has a probability of being picked), and then moves to it.
Alice also has a score, defined as follows:
- Let the vertices she visited be
- Then, she will forget exactly one of these vertices; and her score will be the bitwise xor of the remaining ones.
- That is, if she chooses to forget vertex , then her score is . Here, denotes the bitwise xor operation.
Alice wants to maximize her score, and will always choose to forget a vertex optimally to achieve this.
What is Alice's expected final score?
Find the expected value modulo .
That is, the expected value can be written as for two integers such that ; print the value of .
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains a single integer — the number of vertices of the tree.
- The second line of each test case contains space-separated integers .
- The next lines describe the edges. The of these lines contains two space-separated integers and , denoting an edge between and .
Output Format
For each test case, output on a new line Alice's expected final score.
Constraints
- The sum of over all test cases won't exceed .
Sample 1:
3 3 3 3 1 1 2 2 3 3 2 2 1 1 2 1 3 4 1 2 3 4 1 2 1 3 1 4
2 2 3
Explanation:
Test case : There's only one final vertex Alice can reach, taking the path . She will choose to forget one of the 's, for a final score of .
Test case : There are three leaf nodes, and Alice has a chance of reaching each one.
- If she takes the path , her score is (she will forget )
- If she takes the path , her score is (she will forget )
- If she takes the path , her score is (she will forget )
Her expected score is thus .