結果
問題 |
No.3189 Semifinal Stage
|
ユーザー |
![]() |
提出日時 | 2025-06-20 23:00:11 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,104 bytes |
コンパイル時間 | 3,581 ms |
コンパイル使用メモリ | 298,776 KB |
実行使用メモリ | 22,392 KB |
最終ジャッジ日時 | 2025-06-20 23:01:24 |
合計ジャッジ時間 | 50,532 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | WA * 30 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(),(x).end() #define REP(i, n) for(ll i=0; i<(ll)(n); i++) #define FOR(i, a, b) for(ll i=(ll)(a); i<(b); i++) #define ROF(i, a, b) for(ll i=(ll)(b)-1; i>=(a); i--) template<typename T> int LB(const vector<T>& v, T x) { return lower_bound(ALL(v),x)-(v).begin(); } template<typename T> int UQ(T& v) { sort(ALL(v)); v.erase(unique(ALL(v)),v.end()); return v.size(); } template<typename T> bool chmax(T &a, T b) { return a<b ? a=b, true : false; } template<typename T> bool chmin(T &a, T b) { return a>b ? a=b, true : false; } template<typename T> using rpriority_queue=priority_queue<T,vector<T>,greater<T>>; using ll=long long; const int INF=1e9+10; const ll INFL=4e18; using ld=long double; using ull=unsigned long long; using lll=__int128_t; using VST=vector<string>; using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>; using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>; #ifdef LOCAL #include "./debug.hpp" #else #define debug(...) #define print_line #endif /// @brief LCA struct LCA { /// @brief コンストラクタ LCA(const VVI& g, int root=0) { int n=g.size(); int k=1; while((1<<k)<n) k++; par=VVI(k,VI(n,-1)),dep=VI(n); dfs(g,root,-1); REP(i,k-1) REP(j,n) par[i+1][j]=(par[i][j]==-1?-1:par[i][par[i][j]]); } /// @brief 頂点 u, v のLCAを返す int lca(int u, int v) { if(dep[u]<dep[v]) swap(u,v); int k=par.size(); FOR(i,0,k) if((dep[u]-dep[v])>>i&1) u=par[i][u]; if(u==v) return u; ROF(i,0,k) if(par[i][u]!=par[i][v]) u=par[i][u],v=par[i][v]; return par[0][u]; } /// @brief 頂点 u, v の距離を返す int distance(int u, int v) { return dep[u]+dep[v]-2*dep[lca(u,v)]; } /// @brief 頂点 x が u-v のパス上にあるか否かを返す bool is_on_path(int u, int v, int x) { return distance(u,x)+distance(x,v)==distance(u,v); } /// @brief 頂点 u から d 回遡った位置にある頂点を返す int climb(int u, int d) { int k=par.size(); ROF(i,0,k) if(d>>i&1) u=par[i][u]; return u; } VVI par; VI dep; private: void dfs(const VVI& g, int now, int pre) { par[0][now]=pre; dep[now]=pre==-1?0:dep[pre]+1; for(int nxt:g[now]) if(nxt!=pre) dfs(g,nxt,now); } }; //---------------------------------------------------------- const int B=320; void solve() { ll N; cin>>N; VVI G(N); REP(i,N-1) { ll u,v; cin>>u>>v; u--; v--; G[u].push_back(v); G[v].push_back(u); } LCA lca(G); ll Q; cin>>Q; vector<array<ll,2>> qry(Q); REP(i,Q) cin>>qry[i][0]>>qry[i][1], qry[i][1]--; unordered_set<int> black; for(ll q=0; q*B<Q; q++) { ll qc=min<ll>(B,Q-q*B); unordered_set<int> st, tmp; for(ll i=q*B; i<q*B+qc; i++) { if(qry[i][0]==1) { st.insert(qry[i][1]); } } VL dist(N,INF); queue<int> que; for(ll x: black) if(!st.count(x)) { dist[x]=0; que.push(x); } while(!que.empty()) { int now=que.front(); que.pop(); for(int nxt: G[now]) { if(chmin(dist[nxt],dist[now]+1)) que.push(nxt); } } for(ll i=q*B; i<q*B+qc; i++) { auto [t,v]=qry[i]; if(t==1) { if(black.count(v)) { black.erase(v); if(tmp.count(v)) tmp.erase(v); } else { black.insert(v); tmp.insert(v); } } else { ll ans=dist[v]; for(ll x: tmp) chmin(ans,(ll)lca.distance(x,v)); cout<<ans<<'\n'; } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); //cout<<fixed<<setprecision(15); int T=1; //cin>>T; while(T--) solve(); }