結果

問題 No.2337 Equidistant
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-06-02 23:28:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 567 ms / 4,000 ms
コード長 3,337 bytes
コンパイル時間 1,154 ms
コンパイル使用メモリ 107,808 KB
実行使用メモリ 47,596 KB
最終ジャッジ日時 2023-08-28 06:03:02
合計ジャッジ時間 11,084 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,784 KB
testcase_01 AC 4 ms
13,864 KB
testcase_02 AC 4 ms
13,812 KB
testcase_03 AC 3 ms
13,732 KB
testcase_04 AC 4 ms
13,880 KB
testcase_05 AC 3 ms
13,884 KB
testcase_06 AC 6 ms
13,968 KB
testcase_07 AC 5 ms
14,040 KB
testcase_08 AC 5 ms
14,212 KB
testcase_09 AC 6 ms
13,920 KB
testcase_10 AC 5 ms
13,932 KB
testcase_11 AC 376 ms
32,256 KB
testcase_12 AC 391 ms
32,212 KB
testcase_13 AC 380 ms
32,204 KB
testcase_14 AC 385 ms
32,196 KB
testcase_15 AC 385 ms
32,264 KB
testcase_16 AC 377 ms
32,196 KB
testcase_17 AC 391 ms
32,060 KB
testcase_18 AC 349 ms
32,140 KB
testcase_19 AC 371 ms
32,200 KB
testcase_20 AC 384 ms
32,204 KB
testcase_21 AC 472 ms
47,596 KB
testcase_22 AC 391 ms
32,936 KB
testcase_23 AC 335 ms
32,944 KB
testcase_24 AC 529 ms
42,508 KB
testcase_25 AC 405 ms
32,928 KB
testcase_26 AC 567 ms
42,452 KB
testcase_27 AC 356 ms
32,996 KB
testcase_28 AC 363 ms
32,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


int N;
vector<int> A, B;

vector<vector<int>> graph;
vector<int> par, dep, sz;

void dfs(int u, int p) {
  par[u] = p;
  dep[u] = ~p ? (dep[p] + 1) : 0;
  sz[u] = 1;
  for (const int v : graph[u]) if (p != v) {
    dfs(v, u);
    sz[u] += sz[v];
  }
}

// par, dep

// N <= 2^E
constexpr int E = 18;
int ppar[E][200'010];

int lca(int u, int v) {
  for (int e = E; --e >= 0; ) {
    if (dep[u] - (1 << e) >= dep[v]) {
      u = ppar[e][u];
    }
    if (dep[v] - (1 << e) >= dep[u]) {
      v = ppar[e][v];
    }
  }
  for (int e = E; --e >= 0; ) {
    if (ppar[e][u] != ppar[e][v]) {
      u = ppar[e][u];
      v = ppar[e][v];
    }
  }
  if (u != v) {
    u = ppar[0][u];
    v = ppar[0][v];
  }
  return u;
}

int up(int u, int d) {
  assert(dep[u] >= d);
  for (int e = E; --e >= 0; ) if (d >> e & 1) u = ppar[e][u];
  return u;
}


int main() {
  int Q;
  for (; ~scanf("%d%d", &N, &Q); ) {
    A.resize(N - 1);
    B.resize(N - 1);
    for (int i = 0; i < N - 1; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    
    graph.assign(N, {});
    for (int i = 0; i < N - 1; ++i) {
      graph[A[i]].push_back(B[i]);
      graph[B[i]].push_back(A[i]);
    }
    par.assign(N, -1);
    dep.assign(N, -1);
    sz.assign(N, -1);
    dfs(0, -1);
    copy(par.begin(), par.end(), ppar[0]);
    for (int e = 0; e < E - 1; ++e) {
      for (int u = 0; u < N; ++u) {
        const int p = ppar[e][u];
        ppar[e + 1][u] = (~p) ? ppar[e][p] : -1;
      }
    }
    
    for (int q = 0; q < Q; ++q) {
      int S, T;
      scanf("%d%d", &S, &T);
      --S;
      --T;
      
      int ans = 0;
      const int l = lca(S, T);
      const int ds = dep[S] - dep[l];
      const int dt = dep[T] - dep[l];
      if ((ds + dt) % 2 == 0) {
        const int d = (ds + dt) / 2;
        if (ds > dt) {
          ans += sz[up(S, d)];
          ans -= sz[up(S, d - 1)];
        } else if (ds < dt) {
          ans += sz[up(T, d)];
          ans -= sz[up(T, d - 1)];
        } else {
          ans += N;
          ans -= sz[up(S, d - 1)];
          ans -= sz[up(T, d - 1)];
        }
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
0