XOR paths
Practice
3.8 (12 votes)
Linear algebra
Fast fourier transform
Fourier transformations
Walsh Hadarm transform
Medium Hard
Modular exponentiation
Mathematics
Problem
55% Success 991 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a weighted tree with \(N\) vertices. You can denote the value of path of vertices \(V\) and \(U\) as XOR of weights of all edges on path from \(V\) to \(U\). Your task is to print the number of four vertices \((A, B, C, D)\) such that the value of path of vertices \(A\) and \(B\) differs from the value of path of vertices \(C\) and \(D\) in not more than one bit. 

Input format

  • First line: \(N\) \((1 \leq N \leq 10^5)\)
  • Each of the next \(N-1\) lines: Three space-separated integers \(X, Y, Z\) \((1 \leq X, Y \leq N, \, 0 \leq Z \leq 10^5)\) that represents an edge between vertices \(X\) and \(Y\) with weight \(Z\)

Output format

Print one integer that represents the answer for the problem modulo \(1000000007\) \((10^9+7)\).

Test data

  • In 12.5% of tests: \((1 \leq N \leq 100)\)
  • In 37.5% of tests: \((1 \leq N \leq 1000)\)
  • In 50.0% of tests: \((1 \leq N \leq 100000)\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
MathematicsWalsh-Hadarm transformFast Fourier transformMedium-HardLinear Algebra
Points:50
2 votes
Tags:
Linear AlgebraFast Fourier transformMedium-HardMathematics