// g++-14 1.cpp -std=c++20 -O2 -I . #include using namespace std; #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include using namespace __gnu_pbds; #include using namespace atcoder; using ll = long long; using ld = long double; using vi = vector; using vvi = vector; using vll = vector; using vvll = vector; using vld = vector; using vvld = vector; using vst = vector; using vvst = vector; #define fi first #define se second #define pb push_back #define eb emplace_back #define pq_big(T) priority_queue,less> #define pq_small(T) priority_queue,greater> #define all(a) a.begin(),a.end() #define rep(i,start,end) for(ll i=start;i<(ll)(end);i++) #define per(i,start,end) for(ll i=start;i>=(ll)(end);i--) #define uniq(a) sort(all(a));a.erase(unique(all(a)),a.end()) random_device seed; mt19937_64 randint(seed()); ll grr(ll mi, ll ma) { // [mi, ma) return mi + randint() % (ma - mi); } // https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=7595367#1 // https://atcoder.jp/contests/abc148/submissions/41641665 #include struct lca{ private: using vi = std::vector; using vvi = std::vector; vvi g; vvi ancestor; vi depth; int root; void init(){ ancestor.resize(g.size()); depth.resize(g.size()); dfs(root,-1,0); } void dfs(int now,int prev,int _depth){ depth[now]=_depth; for(int i=prev,x=-1;i!=-1;){ ancestor[now].emplace_back(i); i=++x < (int)ancestor[i].size() ? ancestor[i][x] : -1; } for(auto& next:g[now]){ if(next==prev)continue; dfs(next,now,_depth+1); } } public: lca(vvi& _g,int _r=0) : g(_g),root(_r){ init(); } int deep(int v){ return depth[v]; } int parent(int v){ return v == root ? -1 : ancestor[v][0]; } int kth_ancestor(int v,int k){ if(depth[v]=0;--i){ if((k>>i)&1)v=ancestor[v][i]; } return v; } int query(int u,int v){ if(depth[u]!=depth[v]){ if(depth[u]>depth[v])std::swap(u,v); v=kth_ancestor(v,depth[v]-depth[u]); } if(u==v)return u; for(int i=std::__lg(depth[u]);i>=0;--i){ if(depth[u]<(1<>n; vvi g(n); rep(i,0,n-1){ int u,v;cin>>u>>v; u--;v--; g[u].emplace_back(v); g[v].emplace_back(u); } vi col(n,0); set bl; lca l(g); int q;cin>>q; while(q--){ int t,v;cin>>t>>v; v--; if(t==1){ col[v]^=1; if(col[v])bl.insert(v); else bl.erase(v); } else{ deque> deq; int ans=1e9; if(col[v]){ cout<<0<<"\n"; continue; } if(bl.size()<=300){ for(int pos:bl){ ans=min(ans,l.distance(v,pos)); } cout<