結果

問題 No.2337 Equidistant
ユーザー SSRSSSRS
提出日時 2022-08-22 05:47:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,011 bytes
コンパイル時間 1,748 ms
コンパイル使用メモリ 175,528 KB
実行使用メモリ 23,016 KB
最終ジャッジ日時 2024-04-28 10:56:18
合計ジャッジ時間 9,322 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 166 ms
6,944 KB
testcase_07 AC 180 ms
6,944 KB
testcase_08 AC 161 ms
6,944 KB
testcase_09 AC 163 ms
6,940 KB
testcase_10 AC 160 ms
6,940 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
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);
  }
  for (int i = 0; i < Q; i++){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    vector<int> d1(N, -1);
    d1[A] = 0;
    queue<int> Q1;
    Q1.push(A);
    while (!Q1.empty()){
      int v = Q1.front();
      Q1.pop();
      for (int w : E[v]){
        if (d1[w] == -1){
          d1[w] = d1[v] + 1;
          Q1.push(w);
        }
      }
    }
    vector<int> d2(N, -1);
    d2[B] = 0;
    queue<int> Q2;
    Q2.push(B);
    while (!Q2.empty()){
      int v = Q2.front();
      Q2.pop();
      for (int w : E[v]){
        if (d2[w] == -1){
          d2[w] = d2[v] + 1;
          Q2.push(w);
        }
      }
    }
    int ans = 0;
    for (int j = 0; j < N; j++){
      if (d1[j] == d2[j]){
        ans++;
      }
    }
    cout << ans << endl;
  }
}
0