U621165 情丝断缕,恍如前夜间【陨星】
题目背景
【被数据删除了】。
题目描述
给定一棵 $n$ 个结点的 **有根树**(根节点为 1),对于一条连接树上两个节点的边 $u_i \leftrightarrow v_i$,每条边有一个边权 $w_i$。
下面给出 $q$ 次操作。
- $opt_j = 1$,将点 $u_j$ 与其父亲结点的边断开,移到点 $v_j$ 的下方,并建立一条连接 $u_j$ 与 $v_j$ 的边,边权为 $u_j$ 与原父亲结点所连接的边权。(性质:保证 $u_j$ 为叶子结点。注:所有操作是持久的)。
- $opt_j = 2$,时空逆转,回到第 $id_j$ 次操作后树的状态。
- $opt_j = 3$,查询树上点 $u_j$ 到 点 $v_j$ 的路径权值最大值(注意:$u_j$ 可以等于 $v_j$)。
注:操作一的 $v_j$ 可能等于 $u_j$ 的原父亲。
输入格式
- 第 $1$ 行一个正整数 $n$,代表树的大小。
- 第 $2$ ~ $n$ 行,每行三个正整数 $u_i, v_i, w_i$,代表树上的一条边。
- 第 $n + 1$ ~ $n + q$ 行,每行一次操作。
输出格式
输出每次 $opt_j = 3$ 的查询答案。
说明/提示
$n,q \le 5 \times 10^5,w_i \le 1 \times 10^9$
### 特别提示
本题可能需要卡时间、空间常数,提供快读模板 , 放在 `main` 函数前即可,使用方式:
```cpp line-numbers
read(n , m);
```
支持 `string` 类型。
```cpp
namespace fastio
{
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
inline bool blank(const char x){return!(x^32)||!(x^10)||!(x^13)||!(x^9);}templateinline void read(Tp&x){x=0;register bool z=true;register char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=(x