結果

問題 No.806 木を道に
ユーザー kk
提出日時 2020-07-20 21:00:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 749 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 200,784 KB
実行使用メモリ 10,396 KB
最終ジャッジ日時 2023-08-26 00:41:46
合計ジャッジ時間 5,432 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,652 KB
testcase_01 AC 3 ms
5,700 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 3 ms
5,656 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 3 ms
5,712 KB
testcase_08 AC 3 ms
5,756 KB
testcase_09 AC 3 ms
5,708 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 17 ms
8,892 KB
testcase_25 AC 24 ms
10,396 KB
testcase_26 AC 9 ms
6,832 KB
testcase_27 AC 25 ms
9,096 KB
testcase_28 AC 4 ms
5,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++)
#define ALL(x) (x).begin(), (x).end()

const double PI = acos(-1);

int n;
vector<int> edges[100000];
int dist[100000];

void dfs(int v, int par = -1, int d = 0) {
  dist[v] = d;
  for (int w: edges[v]) {
    if (w != par)
      dfs(w, v, d+1);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  REP (i, n-1) {
    int v, w;
    cin >> v >> w;
    --v, --w;
    edges[v].push_back(w);
    edges[w].push_back(v);
  }

  dfs(0);
  int x = max_element(dist, dist + n) - dist;
  dfs(x);
  int d = *max_element(dist, dist + n);
  cout << n - d - 1 << endl;

  return 0;
}
0