最小异或游走AI (CI翻译)
Problem Statement
题目描述
AI (CI翻译)You are given a simple connected undirected graph with $N$ vertices and $M$ edges. The vertices are numbered $1$ through $N$ and the edges are numbered $1$ through $M$; edge $i$ is an undirected edge of weight $W_i$ connecting vertices $U_i$ and $V_i$.
给定一个有 $N$ 个顶点和 $M$ 条边的简单连通无向图。顶点编号为 $1$ 至 $N$,边编号为 $1$ 至 $M$;边 $i$ 是一条连接顶点 $U_i$ 和 $V_i$、权重为 $W_i$ 的无向边。AI (CI翻译)
The weight of a walk connecting two vertices is defined as the total XOR of the weights of the edges contained in the walk. If the same edge is traversed multiple times, that edge's weight contributes to the total XOR that many times.
连接两个顶点的游走的权重,定义为游走中所经过的边的权重的总异或和。如果同一条边被经过多次,则该边的权重会按经过次数参与异或。AI (CI翻译)
You are given a non-negative integer $K$. Find the number of pairs of integers $(x, y)$ $(1 \leq x \lt y \leq N)$ such that the minimum weight of a walk connecting vertices $x$ and $y$ is at most $K$.
给定一个非负整数 $K$。求满足以下条件的整数对 $(x, y)$ $(1 \leq x \lt y \leq N)$ 的数量:连接顶点 $x$ 和 $y$ 的游走的最小权重不超过 $K$。AI (CI翻译)
$T$ test cases are given; solve each one.
给定 $T$ 个测试用例,请分别求解每个测试用例。AI (CI翻译)
What is a walk?For vertices $X$ and $Y$ in an undirected graph $G$, a sequence of $k$ vertices and $k-1$ edges alternately arranged $(v_1, e_1, v_2, \dots, v_{k-1}, e_{k-1}, v_k)$ such that $v_1 = X$, $v_k = Y$, and for each $i = 1, 2, \dots, k-1$, $e_i$ is an edge connecting $v_i$ and $v_{i+1}$, is called a walk from vertex $X$ to vertex $Y$.
什么是游走?对于无向图 $G$ 中的顶点 $X$ 和 $Y$,若由 $k$ 个顶点和 $k-1$ 条边交替排列组成的序列 $(v_1, e_1, v_2, \dots, v_{k-1}, e_{k-1}, v_k)$ 满足 $v_1 = X$、$v_k = Y$,且对于每个 $i = 1, 2, \dots, k-1$,边 $e_i$ 连接顶点 $v_i$ 和 $v_{i+1}$,则称其为从顶点 $X$ 到顶点 $Y$ 的游走。AI (CI翻译)
What is XOR?The bitwise XOR of non-negative integers $A$ and $B$, $A \oplus B$, is defined as follows. The digit at the $2^k$ ($k \geq 0$) place in the binary representation of $A \oplus B$ is $1$ if exactly one of the digits at the $2^k$ place in the binary representations of $A$ and $B$ is $1$, and $0$ otherwise. For example, $3 \oplus 5 = 6$ (in binary: $011 \oplus 101 = 110$). In general, the bitwise XOR of $k$ non-negative integers $p_1, p_2, p_3, \dots, p_k$ is defined as $(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k)$, and it can be proved that this does not depend on the order of $p_1, p_2, p_3, \dots, p_k$.
什么是 XOR?非负整数 $A$ 和 $B$ 的按位异或 $A \oplus B$ 定义如下:在 $A \oplus B$ 的二进制表示中,$2^k$ ($k \geq 0$) 位上的数字,当且仅当 $A$ 和 $B$ 的二进制表示中 $2^k$ 位上的数字恰有一个为 $1$ 时为 $1$,否则为 $0$。例如,$3 \oplus 5 = 6$(二进制表示为:$011 \oplus 101 = 110$)。一般地,$k$ 个非负整数 $p_1, p_2, p_3, \dots, p_k$ 的按位异或定义为 $(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k)$,并且可以证明其结果与 $p_1, p_2, p_3, \dots, p_k$ 的顺序无关。AI (CI翻译)
Constraints
约束条件
AI (CI翻译)$1 \leq T \leq 10^5$$2 \leq N \leq 2 \times 10^5$$N-1 \leq M \leq 2 \times 10^5$$0 \leq K \lt 2^{30}$$1 \leq U_i \lt V_i \leq N$$0 \leq W_i \lt 2^{30}$- The given graph is simple and connected.
- All input values are integers.
- The sum of
$N$over all test cases in a single input is at most$2 \times 10^5$. - The sum of
$M$over all test cases in a single input is at most$2 \times 10^5$.
$1 \leq T \leq 10^5$$2 \leq N \leq 2 \times 10^5$$N-1 \leq M \leq 2 \times 10^5$$0 \leq K \lt 2^{30}$$1 \leq U_i \lt V_i \leq N$$0 \leq W_i \lt 2^{30}$- 给定的图是简单连通图。
- 所有输入值均为整数。
- 单个输入中所有测试用例的
$N$之和不超过$2 \times 10^5$。 - 单个输入中所有测试用例的
$M$之和不超过$2 \times 10^5$。
Input
输入格式
AI (CI翻译)The input is given from Standard Input in the following format:
输入从标准输入中给出,格式如下:AI (CI翻译)
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_TAI (CI翻译)
Here, $\mathrm{case}_i$ denotes the input for the $i$-th test case. Each test case is given in the following format:
其中,$\mathrm{case}_i$ 表示第 $i$ 个测试用例的输入。每个测试用例的输入格式如下:AI (CI翻译)
N M K
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
N M K
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_MAI (CI翻译)
Output
输出格式
AI (CI翻译)Output $T$ lines. The $i$-th line should contain the answer for the $i$-th test case.
输出 $T$ 行。第 $i$ 行应包含第 $i$ 个测试用例的答案。AI (CI翻译)
样例
3
4 4 2
3 4 3
1 3 4
1 2 3
2 3 2
5 7 14032
1 2 24681
3 5 25665
1 5 14154
2 3 23215
1 3 21259
4 5 24874
3 4 26495
8 10 109312507
6 8 793188457
7 8 501937135
1 2 954888411
2 7 109497327
1 6 791995625
2 6 665857693
1 3 101233808
1 7 114788578
4 6 953503358
5 8 624700613
4
9
22
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。