結果

問題 No.2337 Equidistant
ユーザー gucci0512gucci0512
提出日時 2023-06-02 22:57:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,541 ms / 4,000 ms
コード長 6,659 bytes
コンパイル時間 4,308 ms
コンパイル使用メモリ 245,592 KB
実行使用メモリ 91,808 KB
最終ジャッジ日時 2023-08-28 05:18:22
合計ジャッジ時間 25,171 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
17,564 KB
testcase_01 AC 8 ms
17,572 KB
testcase_02 AC 8 ms
17,556 KB
testcase_03 AC 8 ms
17,636 KB
testcase_04 AC 8 ms
17,576 KB
testcase_05 AC 8 ms
17,548 KB
testcase_06 AC 13 ms
18,092 KB
testcase_07 AC 13 ms
18,020 KB
testcase_08 AC 13 ms
18,096 KB
testcase_09 AC 13 ms
18,092 KB
testcase_10 AC 13 ms
18,128 KB
testcase_11 AC 881 ms
71,276 KB
testcase_12 AC 868 ms
71,196 KB
testcase_13 AC 870 ms
71,248 KB
testcase_14 AC 879 ms
71,208 KB
testcase_15 AC 890 ms
71,296 KB
testcase_16 AC 884 ms
71,288 KB
testcase_17 AC 906 ms
71,220 KB
testcase_18 AC 883 ms
71,272 KB
testcase_19 AC 902 ms
71,240 KB
testcase_20 AC 911 ms
71,308 KB
testcase_21 AC 1,169 ms
91,808 KB
testcase_22 AC 1,424 ms
72,148 KB
testcase_23 AC 806 ms
72,784 KB
testcase_24 AC 1,397 ms
84,728 KB
testcase_25 AC 858 ms
72,768 KB
testcase_26 AC 1,541 ms
84,244 KB
testcase_27 AC 887 ms
72,756 KB
testcase_28 AC 865 ms
72,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
// url 
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=b-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x);
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
typedef long long ll;
typedef long double lld;
using namespace std;
using namespace atcoder;
using mint = static_modint<998244353>;
const ll mod=998244353;
// using mint = static_modint<1000000007>;
//const ll mod=1e9+7;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const string zton="0123456789";
const string atoz="abcdefghijklmnopqrstuvwxyz";
const string ATOZ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll inf=(1ll<<60);
// const int inf=(1<<30);
lld dist(lld x1,lld x2,lld y1,lld y2){
    lld res=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    res=sqrt(abs(res));
    return res;
}
lld arg(lld x,lld y){
    const lld eps=1e-8;
    lld res=0;
    if(abs(x)+abs(y)<=eps)return 0.0;
    else if(abs(x)<=eps){
        if(y>=0.0)return (M_PI/2);
        else return (M_PI/2+M_PI);
    }
    else if(abs(y)<=eps){
        if(x>=0.0)return 0.0;
        else return M_PI;
    }
    res=atan2(abs(y),abs(x));
    if(x<=0&&y>=0)res=(M_PI-res);
    else if(x<=0&&y<=0)res+=(M_PI);
    else if(x>=0&&y<=0)res=(M_PI*2-res);
    return res;
}
ll gcd(ll a,ll b){
    if(a==0||b==0)return a+b;
    ll r;
    r=a%b;
    if(r==0){
        return b;
    }
    else{
        return gcd(b,r);
    }
}
typedef pair<ll,int> P;
struct Edge {
    long long to;
};
using Graph = vector<vector<Edge>>;
/* LCA(G, root): 木 G に対する根を root として Lowest Common Ancestor を求める構造体
    query(u,v): u と v の LCA を求める。計算量 O(logn)
    前処理: O(nlogn)時間, O(nlogn)空間
*/
struct LCA {
    vector<vector<int>> parent;  // parent[k][u]:= u の 2^k 先の親
    vector<int> dist;            // root からの距離
    LCA(const Graph &G, int root = 0) { init(G, root); }
    // 初期化
    void init(const Graph &G, int root = 0) {
        int V = G.size();
        int K = 1;
        while ((1 << K) < V) K++;
        parent.assign(K, vector<int>(V, -1));
        dist.assign(V, -1);
        dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < K; k++) {
            for (int v = 0; v < V; v++) {
                if (parent[k][v] < 0) {
                    parent[k + 1][v] = -1;
                } else {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }
    // 根からの距離と1つ先の頂点を求める
    void dfs(const Graph &G, int v, int p, int d) {
        parent[0][v] = p;
        dist[v] = d;
        for (auto e : G[v]) {
            if (e.to != p) dfs(G, e.to, v, d + 1);
        }
    }
    int query(int u, int v) {
        if (dist[u] < dist[v]) swap(u, v);  // u の方が深いとする
        int K = parent.size();
        // LCA までの距離を同じにする
        for (int k = 0; k < K; k++) {
            if ((dist[u] - dist[v]) >> k & 1) {
                u = parent[k][u];
            }
        }
        // 二分探索で LCA を求める
        if (u == v) return u;
        for (int k = K - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int get_post(int v,int u){
        int K=parent.size();
        int distu=dist[u];
        int distv=dist[v]+1;
        for(int k=K-1;k>=0;k--){
            if(dist[u]-distv>=(1<<k)){
                u=parent[k][u];
            }
        }
        return u;
    }
    int get_half(int s,int t){
        int u=query(s,t);
        int K=parent.size();
        int target_dist=dist[t]-get_dist(s,t)/2;
        for(int k=K-1;k>=0;k--){
            if(dist[t]-target_dist>=(1<<k)){
                t=parent[k][t];
            }
        }
        return t;
    }
    int get_dist(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; }
};
    // int n;cin >> n;
    // Graph g(n);
    // rep(i,0,n){
    //     int k;cin >> k;
    //     rep(j,0,k){
    //         Edge c;
    //         cin >> c.to;
    //         g[i].push_back(c);
    //     }
    // }
    // LCA aa=LCA(g,0);
    // int q;cin >> q;
    // rep(i,0,q){
    //     int u,v;cin >> u >> v;
    //     cout << aa.query(u,v) << endl;
    // }
int N;
vector<int> G[201010];
map<int,int> F[201010];

int dfs(int cu,int pa){
    F[cu][cu]=1;
    for(int to:G[cu]){
        if(to==pa)continue;
        F[cu][to]=dfs(to,cu);
        F[cu][cu]+=F[cu][to];
    }
    return F[cu][cu];
}

int solve(LCA &lca,int s,int t){
    if(s==t){
        return 0;
    }
    int dists=lca.get_dist(s,0);
    int distt=lca.get_dist(t,0);
    if(dists==distt){
        int u=lca.query(s,t);
        int s2=lca.get_post(u,s);
        int t2=lca.get_post(u,t);
        // if(s==14&&t==7){
        //     cout << s2 << " " << t2 << endl;
        // }
        // ans.pb(N-F[u][s2]-F[u][t2]);
        // if(N-F[u][s2]-F[u][t2]<0){
        //     cout << s << " " << t << endl;
        // }
        return N-F[u][s2]-F[u][t2];
    }
    else{
        int distst=lca.get_dist(s,t);
        if(distst%2==1){
            // ans.pb(0);
            return 0;
        }
        else{
            if(dists>distt){
                swap(s,t);
                swap(dists,distt);
            }
            int u=lca.get_half(s,t);
            int t2=lca.get_post(u,t);
            // ans.pb(F[u][u]-F[u][t2]);
            return F[u][u]-F[u][t2];
        }
    }
}

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int Q;cin >> N >> Q;
    
    Graph g(N);
    rep(i,0,N-1){
        int a,b;cin >> a >> b;
        a--;b--;
        Edge c;
        c.to=b;
        g[a].push_back(c);
        c.to=a;
        g[b].push_back(c);
        G[a].pb(b);
        G[b].pb(a);
    }
    dfs(0,-1);
    LCA lca=LCA(g,0);
    vector<int> ans;
    rep(i,0,Q){
        int s,t;cin >> s >> t;s--;t--;
        ans.pb(solve(lca,s,t));
    }
    
    // rep(s,0,N)rep(t,0,N){
    //     int cnt=0;
    //     if(s==t){
    //         continue;
    //     }
    //     rep(u,0,N){
    //         if(u==s||t==u)continue;
    //         if(lca.get_dist(s,u)==lca.get_dist(t,u))cnt++;
    //     }
    //     solve(lca,s,t);
    //     cout << s << " " << t << " : " << solve(lca,s,t) << " " << cnt << endl;

    // }
    for(int x:ans){
        cout << x << endl;
    }
    // rep(i,0,N){
    //     cout << F[i][i] << endl;
    // }
}
0