#LCA0000095. Tổng khoảng cách XOR của mọi cặp đỉnh (Xor Distances)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổng khoảng cách XOR của mọi cặp đỉnh (Xor Distances)
Xor Distances
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected weighted tree with vertices. Each edge has a nonnegative integer weight .
For vertices , let be the bitwise XOR of all edge weights along their unique path, not their arithmetic sum.
Compute the sum of over every unordered pair , counting each pair exactly once. Print the result modulo .
Input
The first line contains . The next lines contain edges .
Output
Print one integer: the sum of XOR distances over all unordered pairs, modulo .
Subtasks
-
Subtask 1 (20 points): ; for every edge; ; edges form a tree.
-
Subtask 2 (30 points): ; for every edge; ; edges form a tree.
-
Subtask 3 (50 points): ; for every edge; ; edges form a tree.
Examples
Input
3
1 2 1
1 3 3
Output
6
Explanation
There are three pairs: , , and . Their sum is .