結果

問題 No.2337 Equidistant
ユーザー FplusFplusFFplusFplusF
提出日時 2023-06-02 22:12:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,016 bytes
コンパイル時間 3,125 ms
コンパイル使用メモリ 211,040 KB
実行使用メモリ 37,256 KB
最終ジャッジ日時 2023-08-28 03:52:39
合計ジャッジ時間 11,378 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 372 ms
37,256 KB
testcase_22 AC 273 ms
30,352 KB
testcase_23 WA -
testcase_24 AC 538 ms
34,784 KB
testcase_25 WA -
testcase_26 AC 595 ms
34,808 KB
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
const ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
struct tree{
    int n,max_k=1;
    vector<vector<int>> g;
    vector<vector<int>> parent;
    vector<int> depth,sz;
    tree(int g_size){
        n=g_size;
        g.resize(n);
    }
    void add_edge(int u,int v){
        g[u].push_back(v);
        g[v].push_back(u);
    }
    void LCA(int root=0){
        while((1<<max_k)<n) max_k++;
        parent.assign(max_k,vector<int>(n,-1));
        depth.assign(n,-1);
        sz.assign(n,0);
        dfs(root,-1,0);
        for(int k=0;k+1<max_k;k++){
            for(int i=0;i<n;i++){
                if(parent[k][i]==-1){
                    parent[k+1][i]=-1;
                }else{
                    parent[k+1][i]=parent[k][parent[k][i]];
                }
            }
        }
    }
    void dfs(int v,int par,int d){
        parent[0][v]=par;
        depth[v]=d;
        sz[v]++;
        for(auto &i:g[v]){
            if(i!=par){
                dfs(i,v,d+1);
                sz[v]+=sz[i];
            }
        }
    }
    int get_lca(int u,int v){
        if(depth[u]<depth[v]) swap(u,v);
        int diff=depth[u]-depth[v];
        for(int k=0;k<max_k;k++){
            if(diff&(1<<k)){
                u=parent[k][u];
            }
        }
        if(u==v) return u;
        for(int k=max_k-1;0<=k;k--){
            if(parent[k][u]!=parent[k][v]){
                u=parent[k][u];
                v=parent[k][v];
            }
        }
        return parent[0][u];
    }
    int get_dist(int u,int v){
        return depth[u]+depth[v]-2*depth[get_lca(u,v)];
    }
    int on_path(int x,int u,int v){
        return get_dist(u,x)+get_dist(x,v)==get_dist(u,v);
    }
    ll d(ll s,ll diff){
        for(int k=0;k<max_k;k++){
            if(diff&(1<<k)){
                s=parent[k][s];
            }
        }
        return s;
    }
    ll q(ll s,ll t){
        if(depth[s]==depth[t]){
            ll u=get_lca(s,t);
            ll a=get_dist(s,u);
            ll b=get_dist(t,u);
            ll x=d(s,a-1);
            ll y=d(t,b-1);
            return sz[u]-sz[x]-sz[y];
        }
        ll k=get_dist(s,t);
        if(k%2!=0) return 0;
        if(depth[s]<depth[t]) swap(s,t);
        s=d(s,k/2-1);
        ll l=parent[0][s];
        return sz[l]-sz[s];
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,q;
    cin >> n >> q;
    tree g(n);
    rep(i,n-1){
        ll a,b;
        cin >> a >> b;
        a--;
        b--;
        g.add_edge(a,b);
    }
    g.LCA(0);
    while(q--){
        ll s,t;
        cin >> s >> t;
        s--;
        t--;
        ll ans=0;
        ans+=g.q(s,t);
        cout << ans << '\n';
    }
}
0