AtCoder Regular Contest 214
B
图中的缺失数字
Missing Number in Graph
AtCoder
⌀ 1451
原题
← AC →

图中的缺失数字AI (CI翻译)

Problem Statement

## 题目描述AI (CI翻译)

There is a connected simple undirected graph with $N$ vertices and $M$ edges, where the vertices are numbered $1, \dots ,N$ and the edges are numbered $1, \dots ,M$. Edge $i$ connects vertices $A_i$ and $B_i$. Also, there are a total of $N+1$ cards, one each with the integers from $0$ to $N$ written on them.

有一个包含 $N$ 个顶点和 $M$ 条边的连通简单无向图,顶点编号为 $1, \dots ,N$,边编号为 $1, \dots ,M$。边 $i$ 连接顶点 $A_i$ 和 $B_i$。此外,还有 $N+1$ 张卡片,卡片上分别写着从 $0$ 到 $N$ 的整数。AI (CI翻译)

Snuke performed the following operations in order:

Snuke 按照以下顺序进行了操作:AI (CI翻译)

  1. Place one card on each vertex, and eat the remaining one card.
  2. For each $i\ (1 \le i \le M)$, write on edge $i$ the bitwise $\mathrm{XOR}$ of the integers written on the two cards placed on vertices $A_i$ and $B_i$. Let this integer be $X_i$.
  3. Discard all cards placed on the vertices.
  1. 在每个顶点上放置一张卡片,并吃掉剩下的一张卡片。
  2. 对于每个 $i\ (1 \leq i \leq M)$,将放置在顶点 $A_i$ 和 $B_i$ 上的两张卡片所写整数的按位 $\mathrm{XOR}$ 写在边 $i$ 上。将该整数记为 $X_i$。
  3. 丢弃放置在顶点上的所有卡片。
AI (CI翻译)

Given the information about the graph ($N,M,A_1,\dots ,A_M,B_1,\dots ,B_M,X_1,\dots ,X_M$), determine the integer written on the card that Snuke ate. If it cannot be uniquely determined, output -1. It is guaranteed that the given $X$ can be obtained by the above operations.

给定图的信息($N,M,A_1,\dots ,A_M,B_1,\dots ,B_M,X_1,\dots ,X_M$),请确定 Snuke 吃掉的卡片上写的整数。如果无法唯一确定,则输出 -1。保证给定的 $X$ 可以通过上述操作得到。AI (CI翻译)

You are given $T$ test cases; solve each of them.

给定 $T$ 组测试数据,请分别求解每组测试数据。AI (CI翻译)

What is bitwise $\mathrm{XOR}$? The bitwise $\mathrm{XOR}$ of non-negative integers $A$ and $B$, $A \oplus B$, is defined as follows: In the binary representation of $A \oplus B$, the digit in the $2^k$ ($k \geq 0$) place is $1$ if exactly one of the digits in 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 $\mathrm{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$.

什么是按位 $\mathrm{XOR}$?对于非负整数 $A$ 和 $B$,按位 $\mathrm{XOR}$ $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$ 的按位 $\mathrm{XOR}$ 定义为 $(\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 \le T \le 10^4$
  • $1 \le N \le 2 \times 10^5$
  • $0 \le M \le 2 \times 10^5$
  • $1 \le A_i,B_i \le N$
  • The given graph is a connected simple undirected graph.
  • $X_1,\dots ,X_M$ can be obtained by the operations in the problem statement.
  • The sum of $N$ over all test cases is at most $2 \times 10^5$.
  • The sum of $M$ over all test cases is at most $2 \times 10^5$.
  • All input values are integers.
  • $1 \leq T \leq 10^4$
  • $1 \leq N \leq 2 \times 10^5$
  • $0 \leq M \leq 2 \times 10^5$
  • $1 \leq A_i,B_i \leq N$
  • 给定的图是连通简单无向图。
  • $X_1,\dots ,X_M$ 可以通过题目描述中的操作得到。
  • 所有测试数据中的 $N$ 之和不超过 $2 \times 10^5$。
  • 所有测试数据中的 $M$ 之和不超过 $2 \times 10^5$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入给出,格式如下:AI (CI翻译)

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
AI (CI翻译)

Each test case is given in the following format:

每组测试数据的格式如下:AI (CI翻译)

N M
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_M B_M X_M

N M
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_M B_M X_M
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output $T$ lines.

输出 $T$ 行。AI (CI翻译)

The $i$-th line should contain the integer written on the card Snuke ate for the $i$-th test case if it can be uniquely determined, and -1 otherwise.

第 $i$ 行应包含第 $i$ 组测试数据中 Snuke 吃掉的卡片上写的整数(如果该整数可以被唯一确定),否则输出 -1。 AI (CI翻译)

样例

Input #1
3
2 1
1 2 3
1 0
4 4
2 1 4
1 3 5
1 4 7
2 3 1
Output #1
0
-1
2
For the first test case, the graph is as shown below. ![image](https://img.atcoder.jp/arc214/3ff6e65b4be3e9a4e483771d6fe27e35.png) The possible pairs of integers written on the cards placed on vertices $1$ and $2$ are $(1,2)$ and $(2,1)$; in either case, the integer written on the card Snuke ate is $0$. For the second test case, there are two possible integers written on the card Snuke ate: $0$ and $1$. For the third test case, the graph is as shown below. ![image](https://img.atcoder.jp/arc214/46b2a2846e7ed1a27553c71b7d760ef8.png)
题目信息
题号
arc214_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。