結果

問題 No.2337 Equidistant
ユーザー 👑 emthrmemthrm
提出日時 2023-06-02 21:40:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,187 ms / 4,000 ms
コード長 3,880 bytes
コンパイル時間 3,752 ms
コンパイル使用メモリ 261,256 KB
実行使用メモリ 49,272 KB
最終ジャッジ日時 2023-08-28 02:59:49
合計ジャッジ時間 15,561 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 434 ms
41,568 KB
testcase_12 AC 425 ms
41,768 KB
testcase_13 AC 439 ms
41,656 KB
testcase_14 AC 432 ms
41,864 KB
testcase_15 AC 441 ms
41,628 KB
testcase_16 AC 438 ms
41,600 KB
testcase_17 AC 433 ms
41,652 KB
testcase_18 AC 446 ms
41,764 KB
testcase_19 AC 442 ms
41,572 KB
testcase_20 AC 441 ms
41,572 KB
testcase_21 AC 644 ms
49,272 KB
testcase_22 AC 356 ms
43,060 KB
testcase_23 AC 360 ms
42,832 KB
testcase_24 AC 912 ms
46,484 KB
testcase_25 AC 384 ms
42,756 KB
testcase_26 AC 1,187 ms
46,444 KB
testcase_27 AC 439 ms
42,720 KB
testcase_28 AC 432 ms
42,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct LowestCommonAncestorByDoubling {
  std::vector<int> depth;
  vector<int> sub;

  explicit LowestCommonAncestorByDoubling(
      const std::vector<std::vector<int>>& graph)
      : is_built(false), n(graph.size()),
        table_h(std::countr_zero(std::bit_floor(graph.size())) + 1),
        graph(graph) {
    depth.resize(n);
    sub.resize(n, 1);
    parent.resize(table_h, std::vector<int>(n));
  }

  void build(int root = 0) {
    is_built = true;
    dfs(-1, root, 0);
    for (int i = 0; i + 1 < table_h; ++i) {
      for (int ver = 0; ver < n; ++ver) {
        parent[i + 1][ver] =
            (parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]);
      }
    }
  }

  int query(int u, int v) const {
    assert(is_built);
    if (depth[u] > depth[v]) std::swap(u, v);
    for (int i = 0; i < table_h; ++i) {
      if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v];
    }
    if (u == v) return u;
    for (int i = table_h - 1; i >= 0; --i) {
      if (parent[i][u] != parent[i][v]) {
        u = parent[i][u];
        v = parent[i][v];
      }
    }
    return parent.front()[u];
  }

  int distance(const int u, const int v) const {
    assert(is_built);
    return depth[u] + depth[v] - depth[query(u, v)] * 2;
  }

  int level_ancestor(int v, const int d) const {
    assert(is_built);
    if (depth[v] < d) return -1;
    for (int i = depth[v] - d, bit = 0; i > 0; i >>= 1, ++bit) {
      if (i & 1) v = parent[bit][v];
    }
    return v;
  }

  int jump(const int u, const int v, const int d) const {
    assert(is_built);
    if (d == 0) [[unlikely]] return u;
    const int l = query(u, v), d_lu = depth[u] - depth[l];
    if (d_lu == d) return l;
    if (d_lu > d) return level_ancestor(u, depth[u] - d);
    return level_ancestor(v, depth[l] + (d - d_lu));
  }

 private:
  bool is_built;
  const int n, table_h;
  const std::vector<std::vector<int>> graph;
  std::vector<std::vector<int>> parent;

  void dfs(const int par, const int ver, const int cur_depth) {
    depth[ver] = cur_depth;
    parent.front()[ver] = par;
    for (const int e : graph[ver]) {
      if (e != par) {
        dfs(ver, e, cur_depth + 1);
        sub[ver] += sub[e];
      }
    }
  }
};

int main() {
  int n, q; cin >> n >> q;
  vector<vector<int>> graph(n);
  REP(_, n - 1) {
    int a, b; cin >> a >> b; --a; --b;
    graph[a].emplace_back(b);
    graph[b].emplace_back(a);
  }
  LowestCommonAncestorByDoubling lca(graph);
  lca.build();
  while (q--) {
    int s, t; cin >> s >> t; --s; --t;
    const int d = lca.distance(s, t);
    if (d % 2 == 1) {
      cout << 0 << '\n';
    } else {
      const int root = lca.jump(s, t, d / 2);
      const int ad_s = lca.jump(root, s, 1), ad_t = lca.jump(root, t, 1);
      if (lca.depth[ad_s] == lca.depth[ad_t]) {
        cout << n - lca.sub[ad_s] - lca.sub[ad_t] << '\n';
      } else {
        const int ch = (lca.depth[ad_s] > lca.depth[ad_t] ? ad_s : ad_t);
        cout << lca.sub[root] - lca.sub[ch] << '\n';
      }
    }
  }
  return 0;
}
0