311397: CF1980G. Yasya and the Mysterious Tree

Memory Limit:512 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

G. Yasya and the Mysterious Treetime limit per test2.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Yasya was walking in the forest and accidentally found a tree with $n$ vertices. A tree is a connected undirected graph with no cycles.

Next to the tree, the girl found an ancient manuscript with $m$ queries written on it. The queries can be of two types.

The first type of query is described by the integer $y$. The weight of each edge in the tree is replaced by the bitwise exclusive OR of the weight of that edge and the integer $y$.

The second type is described by the vertex $v$ and the integer $x$. Yasya chooses a vertex $u$ ($1 \le u \le n$, $u \neq v$) and mentally draws a bidirectional edge of weight $x$ from $v$ to $u$ in the tree.

Then Yasya finds a simple cycle in the resulting graph and calculates the bitwise exclusive OR of all the edges in it. She wants to choose a vertex $u$ such that the calculated value is maximum. This calculated value will be the answer to the query. It can be shown that such a cycle exists and is unique under the given constraints (independent of the choice of $u$). If an edge between $v$ and $u$ already existed, a simple cycle is the path $v \to u \to v$.

Note that the second type of query is performed mentally, meaning the tree does not change in any way after it.

Help Yasya answer all the queries.

Input

The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of queries.

The next $n - 1$ lines of each test case contain three integers $v$, $u$, $w$ ($1 \le v, u \le n$, $1 \le w \le 10^9$) — the ends of some edge in the tree and its weight.

It is guaranteed that the given set of edges forms a tree.

The next $m$ lines of each test case describe the queries:

  • ^ $y$ ($1 \le y \le 10^9$) — parameter of the first type query;
  • ? $v$ $x$ ($1 \le v \le n$, $1 \le x \le 10^9$) — parameters of the second type query.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. The same is guaranteed for $m$.

Output

For each test case, output the answers to the queries of the second type.

ExamplesInput
2
3 7
1 2 1
3 1 8
^ 5
? 2 9
^ 1
? 1 10
^ 6
? 3 1
? 2 9
5 6
1 2 777
3 2 2812
4 1 16
5 3 1000000000
^ 4
? 3 123
? 5 1000000000
^ 1000000000
? 1 908070
? 2 1
Output
13 15 11 10 
1000000127 2812 999756331 999999756 
Input
3
8 4
8 6 3
6 3 4
2 5 4
7 6 2
7 1 10
4 1 4
5 1 2
^ 4
^ 7
? 7 8
? 4 10
5 6
3 1 4
2 3 9
4 3 6
5 2 10
? 5 7
^ 1
^ 8
? 4 10
? 1 9
? 3 6
4 2
2 1 4
4 3 5
2 3 4
^ 13
? 1 10
Output
14 13 
13 8 11 11 
10 

Output

**题目大意:**

Yasya 在森林里发现了一棵有 n 个顶点的树(树是一个无环的连通无向图)。树旁边的古卷上有 m 个查询,查询分为两种类型:

1. 第一种类型的查询由整数 y 描述,树中每条边的权重将被该边权重与整数 y 的按位异或(bitwise XOR)所替换。
2. 第二种类型的查询由顶点 v 和整数 x 描述,Yasya 在树中选择一个顶点 u(1 ≤ u ≤ n,u ≠ v),并想象在 v 和 u 之间绘制一条权重为 x 的双向边。然后,Yasya 在生成的图中找到一个简单循环(simple cycle),并计算循环中所有边的按位异或。她希望选择一个顶点 u,使得计算出的值最大。这个计算出的值将是查询的答案。可以证明,在给定的约束下,这样的循环是存在且唯一的(与 u 的选择无关)。如果 v 和 u 之间已经存在一条边,那么简单循环就是路径 v → u → v。

注意,第二种类型的查询是“心理上”进行的,意味着树在任何方面都不会改变。

帮助 Yasya 回答所有查询。

**输入数据格式:**

第一行包含一个整数 t(1 ≤ t ≤ 10^4)——测试用例的数量。

接下来是每个测试用例的描述。

每个测试用例的第一行包含两个整数 n 和 m(2 ≤ n ≤ 2 × 10^5,1 ≤ m ≤ 2 × 10^5)——树中顶点的数量和查询的数量。

接下来每个测试用例的 n - 1 行包含三个整数 v、u、w(1 ≤ v, u ≤ n,1 ≤ w ≤ 10^9)——树中某条边的两个端点和它的权重。

保证给出的边集形成一棵树。

每个测试用例接下来的 m 行描述查询:

- `^ y`(1 ≤ y ≤ 10^9)——第一种类型查询的参数;
- `? v x`(1 ≤ v ≤ n,1 ≤ x ≤ 10^9)——第二种类型查询的参数。

保证所有测试用例的 n 之和不超过 2 × 10^5。m 也满足同样的条件。

**输出数据格式:**

对于每个测试用例,输出第二种类型查询的答案。**题目大意:** Yasya 在森林里发现了一棵有 n 个顶点的树(树是一个无环的连通无向图)。树旁边的古卷上有 m 个查询,查询分为两种类型: 1. 第一种类型的查询由整数 y 描述,树中每条边的权重将被该边权重与整数 y 的按位异或(bitwise XOR)所替换。 2. 第二种类型的查询由顶点 v 和整数 x 描述,Yasya 在树中选择一个顶点 u(1 ≤ u ≤ n,u ≠ v),并想象在 v 和 u 之间绘制一条权重为 x 的双向边。然后,Yasya 在生成的图中找到一个简单循环(simple cycle),并计算循环中所有边的按位异或。她希望选择一个顶点 u,使得计算出的值最大。这个计算出的值将是查询的答案。可以证明,在给定的约束下,这样的循环是存在且唯一的(与 u 的选择无关)。如果 v 和 u 之间已经存在一条边,那么简单循环就是路径 v → u → v。 注意,第二种类型的查询是“心理上”进行的,意味着树在任何方面都不会改变。 帮助 Yasya 回答所有查询。 **输入数据格式:** 第一行包含一个整数 t(1 ≤ t ≤ 10^4)——测试用例的数量。 接下来是每个测试用例的描述。 每个测试用例的第一行包含两个整数 n 和 m(2 ≤ n ≤ 2 × 10^5,1 ≤ m ≤ 2 × 10^5)——树中顶点的数量和查询的数量。 接下来每个测试用例的 n - 1 行包含三个整数 v、u、w(1 ≤ v, u ≤ n,1 ≤ w ≤ 10^9)——树中某条边的两个端点和它的权重。 保证给出的边集形成一棵树。 每个测试用例接下来的 m 行描述查询: - `^ y`(1 ≤ y ≤ 10^9)——第一种类型查询的参数; - `? v x`(1 ≤ v ≤ n,1 ≤ x ≤ 10^9)——第二种类型查询的参数。 保证所有测试用例的 n 之和不超过 2 × 10^5。m 也满足同样的条件。 **输出数据格式:** 对于每个测试用例,输出第二种类型查询的答案。

加入题单

上一题 下一题 算法标签: