結果

問題 No.2337 Equidistant
ユーザー au7777au7777
提出日時 2023-07-02 21:58:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,249 ms / 4,000 ms
コード長 4,461 bytes
コンパイル時間 4,573 ms
コンパイル使用メモリ 241,952 KB
実行使用メモリ 48,496 KB
最終ジャッジ日時 2024-07-16 17:41:56
合計ジャッジ時間 27,236 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
22,272 KB
testcase_01 AC 30 ms
22,272 KB
testcase_02 AC 31 ms
22,272 KB
testcase_03 AC 31 ms
22,400 KB
testcase_04 AC 30 ms
22,272 KB
testcase_05 AC 31 ms
22,400 KB
testcase_06 AC 39 ms
22,400 KB
testcase_07 AC 35 ms
22,400 KB
testcase_08 AC 33 ms
22,400 KB
testcase_09 AC 35 ms
22,272 KB
testcase_10 AC 35 ms
22,400 KB
testcase_11 AC 928 ms
31,728 KB
testcase_12 AC 908 ms
31,744 KB
testcase_13 AC 918 ms
31,744 KB
testcase_14 AC 892 ms
32,120 KB
testcase_15 AC 888 ms
32,492 KB
testcase_16 AC 873 ms
33,292 KB
testcase_17 AC 891 ms
31,824 KB
testcase_18 AC 890 ms
31,972 KB
testcase_19 AC 889 ms
33,100 KB
testcase_20 AC 889 ms
33,652 KB
testcase_21 AC 1,522 ms
48,496 KB
testcase_22 AC 769 ms
33,496 KB
testcase_23 AC 799 ms
32,732 KB
testcase_24 AC 2,111 ms
43,196 KB
testcase_25 AC 819 ms
32,484 KB
testcase_26 AC 2,249 ms
42,808 KB
testcase_27 AC 847 ms
32,812 KB
testcase_28 AC 853 ms
33,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
typedef long long int ll;
using namespace std;
typedef pair<ll, ll> P;
using namespace atcoder;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define USE998244353
#ifdef USE998244353
const ll MOD = 998244353;
// const double PI = 3.141592653589;
using mint = modint998244353;
#else
const ll MOD = 1000000007;
using mint = modint1000000007;
#endif
const int MAX = 20000001;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll gcd(ll x, ll y) {
   if (y == 0) return x;
   else if (y > x) {
       return gcd (y, x); 
   }
   else return gcd(x % y, y);
}
ll lcm(ll x, ll y) {
   return x / gcd(x, y) * y;
}
ll my_sqrt(ll x) {
    // 
    ll m = 0;
    ll M = 3000000001;
    while (M - m > 1) {
        ll now = (M + m) / 2;
        if (now * now <= x) {
            m = now;
        }
        else {
            M = now;
        }
    }
    return m;
}
ll keta(ll num, ll arity) {
    ll ret = 0;
    while (num) {
        num /= arity;
        ret++;
    }
    return ret;
}
ll ceil(ll n, ll m) {
    // n > 0, m > 0
    ll ret = n / m;
    if (n % m) ret++;
    return ret;
}
ll pow_ll(ll x, ll n) {
    if (n == 0) return 1;
    if (n % 2) {
        return pow_ll(x, n - 1) * x;
    }
    else {
        ll tmp = pow_ll(x, n / 2);
        return tmp * tmp;
    }
}
vector<ll> compress(vector<ll>& v) {
    // [3 5 5 6 1 1 10 1] -> [1 2 2 3 0 0 4 0] 
    vector<ll> u = v;
    sort(u.begin(), u.end());
    u.erase(unique(u.begin(),u.end()),u.end());
    map<ll, ll> mp;
    for (int i = 0; i < u.size(); i++) {
        mp[u[i]] = i;
    }
    for (int i = 0; i < v.size(); i++) {
        v[i] = mp[v[i]];
    }
    return v;
}


int Par[200000];
int dep[200000];
int des[200000];
vector<int> edge[200000];
int dou[200000][18];
int dfs(int cur, int par, int d) {
    dep[cur] = d;
    Par[cur] = par;
    int ret = 1;
    for (auto nex : edge[cur]) {
        if (nex == par) continue;
        ret += dfs(nex, cur, d + 1);
    }
    des[cur] = ret;
    return ret;
}

int anc(int x, int i) {
    for (int bit = 0; bit < 18; bit++) {
        if ((i >> bit) & 1) x = dou[x][bit];
    }
    return x;
}

int common_anc(int x, int y) {
    int M = 200001;
    int m = -1;
    // cout << "Hi " << x << ' ' << y << ' ' << dep[x] << ' ' << dep[y] << '\n';
    if (dep[x] > dep[y]) {
        x = anc(x, dep[x] - dep[y]);
    }
    else {
        y = anc(y, dep[y] - dep[x]);
    }
    // cout << "Hi2 " << x << ' ' << y << '\n';
    assert(dep[x] == dep[y]);
    while (M - m > 1) {
        int mid = (M + m) / 2;
        if (anc(x, mid) == anc(y, mid)) {
            M = mid;
        }
        else {
            m = mid;
        }
    }
    return anc(x, M);
}

int main() {
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    dfs(0, 0, 0);
    for (int i = 0; i < 200000; i++) dou[i][0] = Par[i];
    for (int i = 0; i < 17; i++) {
        for (int j = 0; j < 200000; j++) {
            dou[j][i + 1] = dou[dou[j][i]][i];
        }
    }

    vector<int> ans(q);
    for (int i = 0; i < q; i++) {
        int s, t;
        cin >> s >> t;
        s--;
        t--;
        int p = common_anc(s, t);
        int x = dep[s] - dep[p];
        int y = dep[t] - dep[p];
        if ((x + y) % 2) {
            ans[i] = 0;
            continue;
        }
        int k = (x + y) / 2;
        if (x == y) {
            int z = anc(s, k - 1);
            int w = anc(t, k - 1);
            // cout << s << ' ' << t << ' ' << p << ' ' << z << ' ' << w << endl;
            ans[i] = n - des[z] - des[w];
            continue;
        }
        if (x < y) {
            swap(s, t);
        }
        int z = anc(s, k);
        int w = anc(s, k - 1);
        // cout << s << ' ' << t << ' ' << p << ' ' << z << ' ' << w << endl;
        ans[i] = des[z] - des[w];
    }
    for (auto x : ans) cout << x << endl;
    return 0;
}
0