結果

問題 No.2337 Equidistant
ユーザー RubikunRubikun
提出日時 2023-06-02 21:53:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 453 ms / 4,000 ms
コード長 4,255 bytes
コンパイル時間 2,366 ms
コンパイル使用メモリ 210,120 KB
実行使用メモリ 60,024 KB
最終ジャッジ日時 2023-08-28 03:18:43
合計ジャッジ時間 10,138 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
34,732 KB
testcase_01 AC 11 ms
34,768 KB
testcase_02 AC 11 ms
34,644 KB
testcase_03 AC 11 ms
34,900 KB
testcase_04 AC 11 ms
34,696 KB
testcase_05 AC 11 ms
34,696 KB
testcase_06 AC 13 ms
34,776 KB
testcase_07 AC 13 ms
34,776 KB
testcase_08 AC 12 ms
34,796 KB
testcase_09 AC 13 ms
34,860 KB
testcase_10 AC 13 ms
34,804 KB
testcase_11 AC 270 ms
50,056 KB
testcase_12 AC 261 ms
50,204 KB
testcase_13 AC 274 ms
50,068 KB
testcase_14 AC 270 ms
50,104 KB
testcase_15 AC 275 ms
50,016 KB
testcase_16 AC 257 ms
50,020 KB
testcase_17 AC 270 ms
50,200 KB
testcase_18 AC 261 ms
50,268 KB
testcase_19 AC 268 ms
50,072 KB
testcase_20 AC 259 ms
50,312 KB
testcase_21 AC 294 ms
60,024 KB
testcase_22 AC 326 ms
50,876 KB
testcase_23 AC 234 ms
51,076 KB
testcase_24 AC 381 ms
56,684 KB
testcase_25 AC 246 ms
50,864 KB
testcase_26 AC 453 ms
56,628 KB
testcase_27 AC 274 ms
51,068 KB
testcase_28 AC 271 ms
50,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=400005,INF=1<<30;

struct HeavyLightDecomposition{
    int n;
    vector<int> sz,in,out,nxt,par,depth;
    vector<vector<int>> G;
    
    HeavyLightDecomposition(){}
    
    HeavyLightDecomposition(int n_){
        n=n_;
        sz.assign(n,0);
        in.assign(n,0);
        out.assign(n,0);
        nxt.assign(n,0);
        par.assign(n,0);
        depth.assign(n,0);
        G.assign(n,vector<int>());
    }
    
    void add_edge(int u,int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }
    
    void dfs_sz(int u,int p){
        par[u]=p;
        sz[u]=1;
        if(G[u].size()&&G[u][0]==p) swap(G[u][0],G[u].back());
        for(auto &a:G[u]){
            if(a==p) continue;
            depth[a]=depth[u]+1;
            dfs_sz(a,u);
            sz[u]+=sz[a];
            if(sz[a]>sz[G[u][0]]){
                swap(a,G[u][0]);
            }
        }
    }
    
    void dfs_hld(int u,int p,int &t){
        in[u]=t++;
        for(auto a:G[u]){
            if(a==p) continue;
            nxt[a]=(a==G[u][0] ? nxt[u] : a);
            dfs_hld(a,u,t);
        }
        out[u]=t;
    }
    
    void build(int u){
        int t=0;
        dfs_sz(u,-1);
        dfs_hld(u,-1,t);
    }
    
    int lca(int u,int v){
        if(in[u]>in[v]) swap(u,v);
        if(nxt[u]==nxt[v]) return u;
        return lca(u,par[nxt[v]]);
    }
    
    int mov1(int a,int b){
        if(a==b) return a;
        int c=lca(a,b);
        if(c==a){
            int l=0,r=si(G[a]);
            while(r-l>1){
                int m=(l+r)/2;
                if(par[a]==G[a][m]){
                    if(m+1<r){
                        if(r-l==2){
                            l=m+1;
                            break;
                        }
                        if(in[G[a][m+1]]<=in[b]) l=m+1;
                        else r=m;
                    }else{
                        if(r-l==2){
                            l=m-1;
                            break;
                        }
                        if(in[G[a][m-1]]<=in[b]) l=m-1;
                        else r=m-1;
                    }
                }else{
                    if(in[G[a][m]]<=in[b]) l=m;
                    else r=m;
                }
            }
            if(par[a]!=G[a][l]) return G[a][l];
            else return G[a][l+1];
            //return G[a][l];
        }else{
            return par[a];
        }
    }
    //aからbに向かって1進んだところ
};
//部分木クエリはquery(in[a],out[a])
//点はquery(in[a],in[a]+1)

int par[20][MAX];


int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,Q;cin>>N>>Q;
    HeavyLightDecomposition hld(N);
    
    for(int i=0;i<N-1;i++){
        int a,b;cin>>a>>b;a--;b--;
        hld.add_edge(a,b);
    }
    hld.build(0);
    
    memset(par,-1,sizeof(par));
    for(int i=0;i<N;i++) par[0][i]=hld.par[i];
    for(int t=1;t<20;t++){
        for(int i=0;i<N;i++){
            if(par[t-1][i]!=-1) par[t][i]=par[t-1][par[t-1][i]];
        }
    }
    while(Q--){
        int a,b;cin>>a>>b;a--;b--;
        int c=hld.lca(a,b);
        
        int dis=hld.depth[a]+hld.depth[b]-2*hld.depth[c];
        if(dis&1){
            cout<<0<<"\n";
            continue;
        }
        if(hld.depth[a]<hld.depth[b]) swap(a,b);
        int x=a;
        for(int t=19;t>=0;t--){
            if((dis/2)&(1<<t)){
                x=par[t][x];
            }
        }
        
        int ans=N;
        
        if(x==c){
            ans-=hld.out[hld.mov1(x,a)]-hld.in[hld.mov1(x,a)];
            ans-=hld.out[hld.mov1(x,b)]-hld.in[hld.mov1(x,b)];
        }else{
            ans=hld.out[x]-hld.in[x];
            ans-=hld.out[hld.mov1(x,a)]-hld.in[hld.mov1(x,a)];
        }
        
        cout<<ans<<"\n";
    }
}
0