結果

問題 No.2337 Equidistant
ユーザー SSRSSSRS
提出日時 2022-08-22 05:46:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 844 ms / 4,000 ms
コード長 2,201 bytes
コンパイル時間 2,046 ms
コンパイル使用メモリ 180,140 KB
実行使用メモリ 43,136 KB
最終ジャッジ日時 2024-04-28 10:57:14
合計ジャッジ時間 16,626 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 7 ms
5,376 KB
testcase_07 AC 7 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 6 ms
5,376 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 714 ms
40,364 KB
testcase_12 AC 627 ms
40,328 KB
testcase_13 AC 642 ms
40,292 KB
testcase_14 AC 612 ms
40,368 KB
testcase_15 AC 624 ms
40,364 KB
testcase_16 AC 648 ms
40,392 KB
testcase_17 AC 672 ms
40,384 KB
testcase_18 AC 629 ms
40,352 KB
testcase_19 AC 640 ms
40,512 KB
testcase_20 AC 630 ms
40,376 KB
testcase_21 AC 681 ms
43,136 KB
testcase_22 AC 607 ms
39,364 KB
testcase_23 AC 592 ms
39,536 KB
testcase_24 AC 743 ms
43,132 KB
testcase_25 AC 624 ms
39,464 KB
testcase_26 AC 844 ms
43,136 KB
testcase_27 AC 663 ms
39,488 KB
testcase_28 AC 664 ms
39,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int LOG = 17;
struct lowest_common_ancestor{
  vector<int> d;
  vector<vector<int>> pp;
  lowest_common_ancestor(vector<int> &p, vector<int> &d): d(d){
    int N = p.size();
    pp = vector<vector<int>>(LOG, vector<int>(N, -1));
    pp[0] = p;
    for (int i = 0; i < LOG - 1; i++){
      for (int j = 0; j < N; j++){
        if (pp[i][j] != -1){
          pp[i + 1][j] = pp[i][pp[i][j]];
        }
      }
    }
  }
  int la(int v, int x){
    for (int i = 0; i < LOG; i++){
      if ((x >> i & 1) == 1){
        v = pp[i][v];
      }
    }
    return v;
  }
  int lca(int u, int v){
    if (d[u] > d[v]){
      swap(u, v);
    }
    v = la(v, d[v] - d[u]);
    if (u == v){
      return u;
    }
    for (int i = LOG - 1; i >= 0; i--){
      if (pp[i][u] != pp[i][v]){
        u = pp[i][u];
        v = pp[i][v];
      }
    }
    return pp[0][u];
  }
};
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int U, V;
    cin >> U >> V;
    U--;
    V--;
    E[U].push_back(V);
    E[V].push_back(U);
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  vector<int> d(N, 0);
  queue<int> q;
  q.push(0);
  vector<int> bfs;
  while (!q.empty()){
    int v = q.front();
    q.pop();
    bfs.push_back(v);
    for (int w : E[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        d[w] = d[v] + 1;
        q.push(w);
      }
    }
  }
  reverse(bfs.begin(), bfs.end());
  vector<int> dp(N, 1);
  for (int v : bfs){
    for (int w : c[v]){
      dp[v] += dp[w];
    }
  }
  lowest_common_ancestor T(p, d);
  for (int i = 0; i < Q; i++){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    if (d[A] > d[B]){
      swap(A, B);
    }
    if (d[A] % 2 != d[B] % 2){
      cout << 0 << endl;
    } else {
      int C = T.lca(A, B);
      if (d[A] == d[B]){
        int A2 = T.la(A, d[A] - d[C] - 1);
        int B2 = T.la(B, d[B] - d[C] - 1);
        cout << N - dp[A2] - dp[B2] << endl;
      } else {
        int D = d[A] + d[B] - d[C] * 2;
        int X = T.la(B, D / 2);
        int Y = T.la(B, D / 2 - 1);
        cout << dp[X] - dp[Y] << endl;
      }
    }
  }
}
0