結果

問題 No.2337 Equidistant
ユーザー SSRSSSRS
提出日時 2022-08-22 05:02:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,067 bytes
コンパイル時間 1,759 ms
コンパイル使用メモリ 179,608 KB
実行使用メモリ 43,136 KB
最終ジャッジ日時 2024-04-28 10:56:08
合計ジャッジ時間 15,935 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
5,376 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 709 ms
43,132 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 827 ms
43,132 KB
testcase_25 WA -
testcase_26 AC 810 ms
43,136 KB
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

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 x, y;
    cin >> x >> y;
    x--;
    y--;
    if (d[x] > d[y]){
      swap(x, y);
    }
    int z = T.lca(x, y);
    int D = d[x] + d[y] - d[z] * 2;
    if (D % 2 == 1){
      cout << 0 << endl;
    } else if (d[x] == d[y]){
      cout << N - dp[z] + 1 << endl;
    } else {
      int a = T.la(y, D / 2);
      int b = T.la(y, D / 2 - 1);
      cout << dp[a] - dp[b] << endl;
    }
  }
}
0