結果

問題 No.806 木を道に
ユーザー SSRSSSRS
提出日時 2020-11-27 01:17:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 925 bytes
コンパイル時間 1,708 ms
コンパイル使用メモリ 173,884 KB
実行使用メモリ 9,868 KB
最終ジャッジ日時 2023-10-01 03:44:06
合計ジャッジ時間 5,235 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
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 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 37 ms
6,248 KB
testcase_25 AC 55 ms
8,004 KB
testcase_26 AC 20 ms
5,280 KB
testcase_27 AC 64 ms
9,868 KB
testcase_28 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  vector<int> d1(N, -1);
  d1[0] = 0;
  queue<int> Q1;
  Q1.push(0);
  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);
      }
    }
  }
  int s = 0;
  for (int i = 0; i < N; i++){
    if (d1[i] > d1[s]){
      s = i;
    }
  }
  vector<int> d2(N, -1);
  d2[s] = 0;
  queue<int> Q2;
  Q2.push(s);
  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 mx = 0;
  for (int i = 0; i < N; i++){
    mx = max(mx, d2[i]);
  }
  cout << N - 1 - mx << endl;
}
0