結果

問題 No.3825 Jump
コンテスト
ユーザー D M
提出日時 2026-11-26 09:52:44
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 236 ms / 2,000 ms
+ 937µs
コード長 3,863 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 8,633 ms
コンパイル使用メモリ 640,248 KB
実行使用メモリ 67,144 KB
最終ジャッジ日時 2026-11-26 09:53:04
合計ジャッジ時間 17,468 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
#define rep(i, x, limit) for (long long i = (long long)x; i < (long long)limit; i++)
#define REP(i, x, limit) for (long long i = (long long)x; i <= (long long)limit; i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define el '\n'
#define spa " "
#define Yes cout << "Yes" << el
#define No cout << "No" << el
#define YES cout << "YES" << el
#define NO cout << "NO" << el
#define inp(x) for(auto &i:x)cin>>i
#define eps (1e-10)
#define Equals(a,b) (fabs((a) - (b)) < eps )
#define debug(x) cerr << #x << " = " << x << el
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
using boost::multiprecision::cpp_int;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vb = vector<bool>;
const double pi = 3.141592653589793238;
const int inf = 1073741823;
const ll infl = 1LL << 60;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";
const ll MOD = 998244353;
#include<atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using vm = vector<mint>;
struct Edge {long long to;};
struct S_rmq {long long value; long long index;};
S_rmq op_rmq(S_rmq a, S_rmq b){
    if(a.value < b.value) return a;
    else return b;
}
S_rmq E_rmq(){return {(1LL << 60),-1LL};}

struct euler_tour {
    vector<long long> depth, visit;
    vector<long long> discover, finishing;
    vector<vector<Edge>> g;
    long long n;

    atcoder::segtree<S_rmq, op_rmq, E_rmq> rmq;

    euler_tour(long long n=1, vector<vector<Edge>> g=vector<vector<Edge>>()){
        init(n, g);
    }

    void init(long long n, vector<vector<Edge>> g){
        this->n = n;
        depth.clear();
        visit.clear();
        discover.assign(n, (1<<30));
        finishing.assign(n ,-1);
        this->g = g;
        this->g.resize(n);
    }

    void add_edge(long long u, long long v){
        g[u].push_back({v});
        g[v].push_back({u});
    }
    
    void dfs(long long root){
        Dfs(root, -1, 0);
        for(long long i=0;i<int(visit.size());i++){
            discover[visit[i]] = min(discover[visit[i]], i);
            finishing[visit[i]] = max(finishing[visit[i]], i+1);
        }
        vector<S_rmq> depth_v(int(depth.size()));
        for(long long i=0; i<int(depth.size()); i++){
            depth_v[i] = {depth[i], visit[i]};
        }

        rmq = atcoder::segtree<S_rmq, op_rmq, E_rmq>(depth_v);
    }

    void Dfs(long long v, long long p, long long d) {
        visit.push_back(v);
        depth.push_back(d);
        for (Edge u : g[v]) {
            if (u.to == p) continue;
            Dfs(u.to, v, d + 1);
            visit.push_back(v);
            depth.push_back(d);
        }
    }

    long long lca(long long u, long long v){
        return rmq.prod(min(discover[u], discover[v]), max(finishing[u], finishing[v])).index;
    }

    long long dist(long long u, long long v){
        return depth[discover[u]]+depth[discover[v]]-2*depth[discover[lca(u,v)]];
    }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n,q;cin>>n;
  vs Name={"Alice","Bob"};
  //-5 3   -1 3 -1 1 
  euler_tour et(n);
  rep(i,0,n-1){
    int u,v;cin>>u>>v;
    u--;v--;
    et.add_edge(u,v);
  }
  et.dfs(0);
  cin>>q;
  while(q--){
    int a,b;cin>>a>>b;
    a--;b--;
    int f=et.lca(a,b);
    bool bo=false;
    int x=-et.dist(a,f),y=et.dist(b,f);
    if(x == 0){
    cout << Name[0] << el;
    continue;
}
if(y == 0){
    cout << Name[1] << el;
    continue;
}
    while(x<0&&y>0){
      if(!bo){
        x = x + (y-x)/2;
      }else{
        y = x + (y-x+1)/2;
    }
      bo^=1;
    }
    if(bo)cout<<Name[0]<<el;
    else cout<<Name[1]<<el;
  }
}
0