結果

問題 No.2337 Equidistant
ユーザー merom686merom686
提出日時 2023-06-02 22:35:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,436 bytes
コンパイル時間 2,597 ms
コンパイル使用メモリ 205,428 KB
実行使用メモリ 29,224 KB
最終ジャッジ日時 2023-08-28 04:39:09
合計ジャッジ時間 9,022 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,380 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 334 ms
29,224 KB
testcase_22 AC 162 ms
22,684 KB
testcase_23 WA -
testcase_24 AC 446 ms
27,112 KB
testcase_25 WA -
testcase_26 AC 507 ms
26,796 KB
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
        //cout << v[s].d << ' ' << v[t].d << ' ' << v[i].d << endl;

        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