結果

問題 No.2337 Equidistant
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-06-02 23:26:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,341 bytes
コンパイル時間 1,085 ms
コンパイル使用メモリ 107,632 KB
実行使用メモリ 47,464 KB
最終ジャッジ日時 2023-08-28 05:59:54
合計ジャッジ時間 10,930 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,800 KB
testcase_01 WA -
testcase_02 AC 3 ms
13,804 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 4 ms
13,796 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 459 ms
47,464 KB
testcase_22 AC 355 ms
32,752 KB
testcase_23 WA -
testcase_24 AC 581 ms
42,524 KB
testcase_25 WA -
testcase_26 AC 559 ms
42,360 KB
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

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 += sz[l];
          ans -= sz[up(S, d - 1)];
          ans -= sz[up(T, d - 1)];
        }
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
0