#abc201e. E - Xor Distances

E - Xor Distances

Score : 500500 points

问题描述

我们有一个包含 NN 个顶点的加权树。第 ii 条边双向连接顶点 uiu_i 和顶点 viv_i,且具有权重 wiw_i

对于一对顶点 (x,y)(x, y),我们定义 dist(x,y)\text{dist}(x, y) 如下:

  • xxyy 的最短路径上边权重的 异或 运算结果。

求出所有满足条件 1i<jN1 \leq i < j \leq N 的顶点对 (i,j)(i, j)dist(i,j)\text{dist}(i,j) 值,并将这些值的和对 (109+7)(10^9+7) 取模输出。

什么是  XOR \text{ XOR }

整数 AABB 的按位 XOR\mathrm{XOR}(异或)运算,记作 A XOR BA\ \mathrm{XOR}\ B,定义如下:

  • A XOR BA\ \mathrm{XOR}\ B 转换为二进制表示时,其在 2k2^k 位置上的数字(k0k \geq 0)是 11,当且仅当 AABB 中恰好有一个为 11,否则为 00

例如,我们有 3 XOR 5=63\ \mathrm{XOR}\ 5 = 6(以二进制表示:011 XOR 101=110011\ \mathrm{XOR}\ 101 = 110)。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a weighted tree with NN vertices. The ii-th edge connects Vertex uiu_i and Vertex viv_i bidirectionally and has a weight wiw_i.

For a pair of vertices (x,y)(x,y), let us define dist(x,y)\text{dist}(x,y) as follows:

  • the XOR of the weights of the edges in the shortest path from xx to yy.

Find dist(i,j)\text{dist}(i,j) for every pair (i,j)(i,j) such that 1i<jN1 \leq i \lt j \leq N, and print the sum of those values modulo (109+7)(10^9+7).

What is  XOR \text{ XOR }?

The bitwise XOR\mathrm{XOR} of integers AA and BB, A XOR BA\ \mathrm{XOR}\ B, is defined as follows:

  • When A XOR BA\ \mathrm{XOR}\ B is written in base two, the digit in the 2k2^k's place (k0k \geq 0) is 11 if exactly one of AA and BB is 11, and 00 otherwise.

For example, we have 3 XOR 5=63\ \mathrm{XOR}\ 5 = 6 (in base two: 011 XOR 101=110011\ \mathrm{XOR}\ 101 = 110).

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1ui<viN1 \leq u_i \lt v_i \leq N
  • 0wi<2600 \leq w_i \lt 2^{60}
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

u1u_1 v1v_1 w1w_1

u2u_2 v2v_2 w2w_2

\vdots

uN1u_{N-1} vN1v_{N-1} wN1w_{N-1}

Output

Print the sum of dist(i,j)\text{dist}(i,j), modulo (109+7)(10^9+7).

Sample Input 1

3
1 2 1
1 3 3

Sample Output 1

6

We have dist(1,2)=1,\text{dist}(1,2)=1, dist(1,3)=3,\text{dist}(1,3)=3, and dist(2,3)=2\text{dist}(2,3)=2, for the sum of 66.

Sample Input 2

5
3 5 2
2 3 2
1 5 1
4 5 13

Sample Output 2

62

Sample Input 3

10
5 7 459221860242673109
6 8 248001948488076933
3 5 371922579800289138
2 5 773108338386747788
6 10 181747352791505823
1 3 803225386673329326
7 8 139939802736535485
9 10 657980865814127926
2 4 146378247587539124

Sample Output 3

241240228

Print the sum modulo (109+7)(10^9+7).

update @ 2024/3/10 09:13:40