#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; struct HLD { const vector>& to; int root, n; vector sz, parent, depth, idx, ridx, head, inv; HLD(const vector>& to, int root=0) : to(to), root(root), n(to.size()), sz(n), parent(n), depth(n), idx(n), ridx(n), head(n), inv(n) { init_tree_data(root, -1, 0); int nxt = 0; assign_idx(root, root, nxt); } void init_tree_data(int u, int p, int d) { parent[u] = p; depth[u] = d; int s = 1; for (int v: to[u]) if (v != p) { init_tree_data(v, u, d+1); s += sz[v]; } sz[u] = s; } void assign_idx(int u, int h, int& nxt, int p=-1) { head[u] = h; idx[u] = nxt; inv[nxt] = u; nxt++; int heaviest = -1; int mxweight = 0; for (int v: to[u]) if (v != p) { if (sz[v] > mxweight) { heaviest = v; mxweight = sz[v]; } } if (heaviest != -1) { assign_idx(heaviest, h, nxt, u); for (int v: to[u]) if (v != p && v != heaviest) { assign_idx(v, v, nxt, u); } } ridx[u] = nxt; } int lca(int u, int v) { while (head[u] != head[v]) { if (depth[head[u]] > depth[head[v]]) { u = parent[head[u]]; } else { v = parent[head[v]]; } } return depth[u] < depth[v] ? u : v; } // returns reference to tuple of (path fragments from x upto lca (excluding lca), those from y, lca) // storage of retval is reused to avoid creating short vectors on each query tuple>, vector>, int> paths_res; auto& paths(int x, int y) { auto& [x_paths, y_paths, lca] = paths_res; x_paths.clear(); y_paths.clear(); while (head[x] != head[y]) { int hx = head[x], hy = head[y]; if (depth[hx] > depth[hy]) { x_paths.emplace_back(x, hx); x = parent[hx]; } else { y_paths.emplace_back(y, hy); y = parent[hy]; } } if (depth[x] > depth[y]) { x_paths.emplace_back(x, inv[idx[y] + 1]); x = y; } else if (depth[x] < depth[y]) { y_paths.emplace_back(y, inv[idx[x] + 1]); y = x; } lca = x; return paths_res; } int dist(int u, int v) { int w = lca(u, v); return depth[u] + depth[v] - 2 * depth[w]; } template int max_ancestor(int v, F f) { if (!f(v)) return -1; int hv = head[v]; int p = parent[hv]; while (p != -1 && f(p)) { v = p; hv = head[v]; p = parent[hv]; } int il = idx[hv] - 1, ir = idx[v]; while (ir - il > 1) { int ic = (il + ir) / 2; (f(inv[ic]) ? ir : il) = ic; } return inv[ir]; } int ascend(int v, int k) { assert(depth[v] >= k); int td = depth[v] - k; int hv = head[v]; while (depth[hv] > td) { v = parent[hv]; hv = head[v]; } int rest = depth[v] - td; return inv[idx[v] - rest]; } int move_to(int u, int v, int by) { int l = lca(u, v); int du = depth[u] - depth[l]; int dv = depth[v] - depth[l]; assert(by <= du + dv); if (by <= du) return ascend(u, by); else return ascend(v, du + dv - by); } }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; VVI to(n); rep(_, n - 1) { int a, b; cin >> a >> b; a--, b--; to[a].emplace_back(b); to[b].emplace_back(a); } HLD hld(to); segtree used(2 * n), dist(2 * n); VI col(n); auto op = [&](int u) { col[u] ^= 1; used.set(hld.idx[u], col[u]); used.set(hld.idx[u] + n, col[u]); for (int p : {hld.idx[u], hld.idx[u] + n}) { int pre = used.min_left(p, [](int x) { return !x; }) - 1; int nxt = used.max_right(p+1, [](int x) { return !x; }); if (col[u]) { if (pre >= 0) dist.set(pre, hld.dist(hld.inv[pre%n], hld.inv[p%n])); if (nxt < 2*n) dist.set(p, hld.dist(hld.inv[p%n], hld.inv[nxt%n])); } else { dist.set(p, 0); if (pre >= 0 && nxt < 2*n) dist.set(pre, hld.dist(hld.inv[pre%n], hld.inv[nxt%n])); } } }; rep(u, n) { int c; cin >> c; if (c) op(u); } int q; cin >> q; rep(_, q) { int type; cin >> type; if (type == 1) { int v; cin >> v; op(v-1); } else { int x, y; cin >> x >> y; x--, y--; int l, r; if (x == y) { l = 0, r = n; } else { if (hld.lca(x, y) == y) { int rv = hld.ascend(x, hld.depth[x] - hld.depth[y] - 1); l = hld.ridx[rv], r = hld.idx[rv] + n; } else { l = hld.idx[y], r = hld.ridx[y]; } } int last = used.min_left(r, [](int x) { return !x; }) - 1; if (last < l) { cout << 0 << '\n'; continue; } int first = used.max_right(l, [](int x) { return !x; }); int ans = (dist.prod(first, last) + hld.dist(hld.inv[first%n], hld.inv[last%n])) / 2 + 1; cout << ans << '\n'; } } }