树链剖分偏模板题。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 100000 + 7;
const int INF = ~0U >> 1;
typedef long long LL;
typedef pair<int, int> P;
struct node {
int l, r;
int sum;
} s[maxn<<2];
void pushup(int rt) {
s[rt].sum = s[rt << 1].sum + s[rt * 2 + 1].sum;
}
void build(int rt, int l, int r) {
s[rt].l = l;
s[rt].r = r;
s[rt].sum = 0;
if(l == r) return ;
int mid = (l + r) / 2;
build(rt * 2, l, mid);
build(rt * 2 + 1, mid + 1, r);
}
void add(int rt, int idx, int val) {
if(s[rt].l == s[rt].r) {
s[rt].sum += val;
return ;
}
int mid = (s[rt].l + s[rt].r) / 2;
if(idx <= mid) add(rt * 2, idx, val);
else add(rt * 2 + 1, idx, val);
pushup(rt);
}
int query(int rt, int l, int r) {
if(s[rt].l == l && s[rt].r == r) return s[rt].sum;
int mid = (s[rt].l + s[rt].r) / 2;
if(l > mid) return query(rt * 2 + 1, l, r);
else if(r <= mid) return query(rt * 2, l, r);
else return query(rt * 2, l, mid) + query(rt * 2 + 1, mid + 1, r);
}
/**
siz[v]表示以v为根的子树的节点总数。
dep[v]表示v的深度。
son[v]表示与v在同一重链上的v的儿子节点。
fa[v]表示v的父亲节点。
top[v]表示v所在链的顶端节点。
w[v]表示节点v在线段树中的位置。
**/
struct Edge {
int to, next;
} edge[maxn<<2];
int p[maxn], cnt = 0, tot = 0;
int siz[maxn], top[maxn], son[maxn], d[maxn], fa[maxn];
int w[maxn];
void init() {
tot = 0;
memset(d, 0, sizeof(d));
memset(siz, 0, sizeof(siz));
memset(son, 0, sizeof(son));
memset(p, -1, sizeof(p));
}
void addEdge(int from, int to) {
edge[cnt].to = to;
edge[cnt].next = p[from];
p[from] = cnt++;
}
void dfs1(int u, int pre) {
siz[u] = 1;
fa[u] = pre;
son[u] = 0;
d[u] = d[pre] + 1;
for(int t = p[u]; t + 1; t = edge[t].next) {
int v = edge[t].to;
if(v == pre) continue;
dfs1(v, u);
siz[u] += siz[v];
if(siz[son[u]] < siz[v])
son[u] = v;
}
}
void dfs2(int u, int pre) {
w[u] = ++tot;
top[u] = pre;
if(son[u]) dfs2(son[u], top[u]);
for(int t = p[u]; t + 1; t = edge[t].next) {
int v = edge[t].to;
if(v != fa[u] && v != son[u]) dfs2(v, v);
}
}
int LCA(int a, int b) {
while (true) {
if (top[a] == top[b])
return d[a] <= d[b] ? a : b;
else if (d[top[a]] >= d[top[b]])
a = fa[top[a]];
else b = fa[top[b]];
}
}
void Swap(int &a, int &b) {
int t = a;
a = b;
b = t;
}
int doing(int a, int b) {
int f_a = top[a], f_b = top[b];
int dis = 0;
while(f_a != f_b) {
if(d[f_a] < d[f_b]) {
Swap(f_a, f_b);
Swap(a, b);
}
dis += w[a] - w[f_a] + 1;
int t = query(1, w[f_a], w[a]);
if(t) return -1;
a = fa[f_a];
f_a = top[a];
}
if(a == b) return dis;
else {
if(d[a] > d[b]) Swap(a, b);
int t = query(1, w[son[a]], w[b]);
if(t) return -1;
return dis + w[b] - w[a];
}
}
int e[maxn][3];
int n, q;
int main() {
scanf("%d", &n);
init();
for(int i = 1; i < n; ++i) {
scanf("%d%d", &e[i][0], &e[i][1]);
addEdge(e[i][0], e[i][1]);
addEdge(e[i][1], e[i][0]);
}
build(1, 1, n);
dfs1(1, 0);
dfs2(1, 1);
scanf("%d", &q);
int ty, u, v;
for(int i = 0; i < q; ++i) {
scanf("%d", &ty);
if(ty == 3) {
scanf("%d%d", &u, &v);
printf("%d\n", doing(u, v));
} else {
scanf("%d", &u);
int t;
if(d[e[u][0]] > d[e[u][1]])
t = e[u][0];
else t = e[u][1];
if(ty == 1) add(1, w[t], -1);
else add(1, w[t], 1);
}
}
return 0;
}