結果

問題 No.3189 Semifinal Stage
ユーザー Today03
提出日時 2025-06-20 22:51:51
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 4,283 bytes
コンパイル時間 4,246 ms
コンパイル使用メモリ 298,884 KB
実行使用メモリ 22,140 KB
最終ジャッジ日時 2025-06-20 22:52:32
合計ジャッジ時間 29,268 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 PER(i, n) for(ll i=(ll)(n)-1; i>=0; 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 lll=__int128_t; using ull=unsigned long long;
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
/// @ref verify: https://onlinejudge.u-aizu.ac.jp/status/users/Today03/submissions/1/GRL_5_C/judge/10572843/C++17
struct LCA {
    /// @brief コンストラクタ
    LCA(const vector<vector<int>>& g, int root=0) {
        int n=g.size();
        int k=1;
        while((1<<k)<n) k++;
        par=vector<vector<int>>(k,vector<int>(n,-1)),dep=vector<int>(n);
        dfs(g,root,-1);
        for(int i=0; i<k-1; i++) for(int j=0; j<n; j++) 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(int i=0; i<k; i++) if((dep[u]-dep[v])>>i&1) u=par[i][u];
        if(u==v) return u;
        for(int i=k-1; i>=0; i--) 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();
        for(int i=k-1; i>=0; i--) if(d>>i&1) u=par[i][u];
        return u;
    }

    vector<vector<int>> par;
    vector<int> dep;
private:
    void dfs(const vector<vector<int>>& 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)) {
            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(now);
            }
        }

        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();
}
0