EvenOddity

4

3 votes
Trees, Bit Manipulation, Algorithms, Dynamic Programming
Problem

In a quaint forest, a magnificent tree with N vertices, each connected by N1 enchanting edges where each edge has some value.
The goal is to count the number of simple paths having the following intriguing properties: 

  1. The number of set bits (1's) in the Bitwise XOR result of all the edge values on the simple path is even.
  2. The length of the simple path is odd (i.e., the count of edges in the simple path is odd).

Note: A simple path is a path in a tree that does not have repeating vertices. A simple path from node u to node v is considered different than a simple path from node v to node u.

Input format

  • The first line contains a single integer T, which denotes the number of test cases.
  • For each test case:
    • The first line contains N denoting the number of vertices in the tree.
    • The following N1 lines contain 3 space-separated integers, uv, and w indicating that there is an edge between vertices u & v, and this edge has value w.

Output format

For each test case, print the number of simple paths having the aforementioned intriguing properties in a new line.

Constraints

1T1053N1061u,vN0w109The sum of all values of N over all test cases doesn't exceed 106

 

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

The first line denotes T = 1.

For test case 1:

We are given:

  • N = 5, and the tree is as follows:

                

Now, the simple paths that have an odd number of edges and an even number of set bits (1's) in the Bitwise XOR result of all the edge values on the simple path are as follows

  • 2135
  • 5312
  • 12
  • 21
  • 34
  • 43

Therefore, the answer is 6.

Editor Image

?