結果

問題 No.2337 Equidistant
ユーザー merom686merom686
提出日時 2023-06-02 22:38:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 385 ms / 4,000 ms
コード長 2,372 bytes
コンパイル時間 2,844 ms
コンパイル使用メモリ 205,084 KB
実行使用メモリ 29,116 KB
最終ジャッジ日時 2023-08-28 04:47:26
合計ジャッジ時間 8,426 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 186 ms
22,628 KB
testcase_12 AC 191 ms
22,716 KB
testcase_13 AC 196 ms
22,560 KB
testcase_14 AC 190 ms
22,616 KB
testcase_15 AC 195 ms
22,580 KB
testcase_16 AC 193 ms
22,640 KB
testcase_17 AC 193 ms
22,508 KB
testcase_18 AC 190 ms
22,636 KB
testcase_19 AC 185 ms
22,560 KB
testcase_20 AC 195 ms
22,660 KB
testcase_21 AC 253 ms
29,116 KB
testcase_22 AC 150 ms
22,688 KB
testcase_23 AC 173 ms
22,620 KB
testcase_24 AC 370 ms
27,052 KB
testcase_25 AC 173 ms
22,576 KB
testcase_26 AC 385 ms
27,068 KB
testcase_27 AC 174 ms
22,564 KB
testcase_28 AC 176 ms
22,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using namespace std;

struct Graph {
    static constexpr int N = 18; // n <= 2^N
    struct Vertex { int n, d, k, p[N]; };
    struct Edge { int i, n; };
    Graph(int n, int m) : v(n, { -1, -1, -1, {} }), e(m), n(n), m(0) {}
    void add_edge(int i, int j) {
        e[m] = { j, v[i].n };
        v[i].n = m;
        m++;
    }
    int dfs(int i, int p, int d) {
        v[i].d = d;
        v[i].p[0] = p;
        int k = 1;
        for (int j = v[i].n; j >= 0; j = e[j].n) {
            Edge &o = e[j];
            if (o.i == p) continue;
            k += dfs(o.i, i, d + 1);
        }
        return v[i].k = k;
    }
    void doubling() {
        dfs(0, 0, 0);
        for (int k = 1; k < N; k++) {
            for (int i = 0; i < n; i++) {
                v[i].p[k] = v[v[i].p[k - 1]].p[k - 1];
            }
        }
    }
    int f() {
        int s, t;
        cin >> s >> t;
        s--; t--;

        int d = v[s].d - v[t].d;
        if (d < 0) d *= -1, swap(s, t);

        int i = s, j = t;
        if (d > 0) {
            for (int k = N - 1; k >= 0; k--) {
                if (d & 1 << k) i = v[i].p[k];
            }
        }
        if (i != j) {
            for (int k = N - 1; k >= 0; k--) {
                if (v[i].p[k] == v[j].p[k]) continue;
                i = v[i].p[k];
                j = v[j].p[k];
            }
            i = v[i].p[0];
        }
        int l = v[s].d + v[t].d - v[i].d * 2;
        if (l % 2 != 0) return 0;

        l /= 2;
        l--;
        if (l > 0) {
            for (int k = N - 1; k >= 0; k--) {
                if (l & 1 << k) s = v[s].p[k];
            }
        }
        int u = v[s].p[0];
        int k = v[u].k - v[s].k;
        if (d == 0) {
            for (int k = N - 1; k >= 0; k--) {
                if (l & 1 << k) t = v[t].p[k];
            }
            return n - v[s].k - v[t].k;
        }
        return k;
    }
    vector<Vertex> v;
    vector<Edge> e;
    int n, m;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, q;
    cin >> n >> q;

    Graph g(n, (n - 1) * 2);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;

        g.add_edge(a, b);
        g.add_edge(b, a);
    }
    g.doubling();

    while (q--) {
        cout << g.f() << '\n';
    }

    return 0;
}
0