#include using namespace std; struct NodeAgg { int cnt = 0; int first = -1; // vertex id int last = -1; // vertex id long long sum = 0; // sum of dist between consecutive active nodes in-order (linear) }; static const int MAXLOG = 20; int N; vector> g; vector depthv; int up[MAXLOG][200000 + 5]; int tin[200000 + 5], tout[200000 + 5], euler[200000 + 5]; int timer_ = 0; bool is_ancestor(int a, int b){ // a is ancestor of b in rooted tree return tin[a] <= tin[b] && tout[b] <= tout[a]; } int jump_up(int v, int k){ for(int j=0;j=0;j--){ int nv = up[j][v]; if(nv != 0 && !is_ancestor(nv, b)) v = nv; } return up[0][v]; } long long dist_uv(int a, int b){ int c = lca(a,b); return (long long)depthv[a] + depthv[b] - 2LL*depthv[c]; } struct SegTree { int n; // size (power of two) vector st; // 1-indexed internal or 0-indexed iterative static NodeAgg mergeAgg(const NodeAgg &L, const NodeAgg &R){ if(L.cnt == 0) return R; if(R.cnt == 0) return L; NodeAgg res; res.cnt = L.cnt + R.cnt; res.first = L.first; res.last = R.last; res.sum = L.sum + R.sum + dist_uv(L.last, R.first); return res; } SegTree(int sz=0){ init(sz); } void init(int sz){ n = 1; while(n < sz) n <<= 1; st.assign(2*n, NodeAgg()); } void build_from_active(const vector &active_by_tin){ // active_by_tin is size N+1 (1..N), position = tin vertex for(int i=0;i=1;i--){ st[i] = mergeAgg(st[i<<1], st[i<<1|1]); } } void update_pos(int pos0, int vertex_or_minus1){ // pos0: 0-indexed position in euler order NodeAgg leaf; if(vertex_or_minus1 != -1){ leaf.cnt = 1; leaf.first = leaf.last = vertex_or_minus1; leaf.sum = 0; } int i = n + pos0; st[i] = leaf; for(i >>= 1; i >= 1; i >>= 1){ st[i] = mergeAgg(st[i<<1], st[i<<1|1]); if(i==1) break; } } NodeAgg query_range(int l, int r){ // [l, r) 0-indexed, order-sensitive NodeAgg leftAcc, rightAcc; for(l += n, r += n; l < r; l >>= 1, r >>= 1){ if(l & 1) leftAcc = mergeAgg(leftAcc, st[l++]); if(r & 1) rightAcc = mergeAgg(st[--r], rightAcc); } return mergeAgg(leftAcc, rightAcc); } }; long long steiner_vertices_from_agg(const NodeAgg &agg){ if(agg.cnt == 0) return 0; if(agg.cnt == 1) return 1; long long cycle = agg.sum + dist_uv(agg.last, agg.first); long long edges = cycle / 2; return edges + 1; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; g.assign(N+1, {}); for(int i=0;i> a >> b; g[a].push_back(b); g[b].push_back(a); } vector color(N+1); for(int i=1;i<=N;i++) cin >> color[i]; depthv.assign(N+1, 0); // DFS iterative to fill tin/tout, up[0], depth, euler array // root at 1, parent of root = 0 vector parent(N+1,0); vector it(N+1,0); vector stv; stv.reserve(N); stv.push_back(1); parent[1]=0; up[0][1]=0; depthv[1]=0; while(!stv.empty()){ int v = stv.back(); if(it[v]==0){ tin[v]=timer_; euler[timer_] = v; timer_++; } if(it[v] < (int)g[v].size()){ int to = g[v][it[v]++]; if(to==parent[v]) continue; parent[to]=v; up[0][to]=v; depthv[to]=depthv[v]+1; stv.push_back(to); }else{ tout[v]=timer_-1; stv.pop_back(); } } for(int j=1;j active(N+1,0); for(int i=1;i<=N;i++) active[i] = (color[i]==1); seg.build_from_active(active); auto query_subtree = [&](int u)->long long{ int l = tin[u]; int r = tout[u] + 1; NodeAgg agg = seg.query_range(l, r); return steiner_vertices_from_agg(agg); }; auto query_complement_subtree = [&](int u)->long long{ int l = tin[u]; int r = tout[u] + 1; NodeAgg left = seg.query_range(0, l); NodeAgg right = seg.query_range(r, N); NodeAgg merged = SegTree::mergeAgg(left, right); return steiner_vertices_from_agg(merged); }; int Q; cin >> Q; while(Q--){ int t; cin >> t; if(t==1){ int v; cin >> v; color[v] ^= 1; int pos = tin[v]; if(color[v]==1){ seg.update_pos(pos, v); }else{ seg.update_pos(pos, -1); } }else{ int x,y; cin >> x >> y; if(x==y){ NodeAgg agg = seg.query_range(0, N); cout << steiner_vertices_from_agg(agg) << "\n"; continue; } if(is_ancestor(y, x)){ // neighbor z is the child of y on path to x int z = jump_up(x, depthv[x] - depthv[y] - 1); cout << query_complement_subtree(z) << "\n"; }else{ // component containing x is outside subtree(y), so S = subtree(y) cout << query_subtree(y) << "\n"; } } } return 0; }