結果

問題 No.806 木を道に
ユーザー niuezniuez
提出日時 2019-03-22 21:36:22
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 822 bytes
コンパイル時間 1,575 ms
コンパイル使用メモリ 171,124 KB
実行使用メモリ 10,612 KB
最終ジャッジ日時 2023-10-19 06:35:17
合計ジャッジ時間 3,699 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 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 39 ms
7,972 KB
testcase_25 AC 60 ms
10,612 KB
testcase_26 AC 21 ms
5,472 KB
testcase_27 AC 68 ms
10,568 KB
testcase_28 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

vector<vector<int>> g;

int last;

void dfs(int v, int f) {
  last = v;
  for(int to : g[v]) {
    if(f == to) continue;
    dfs(to, v);
  }
}

vector<int> dist;

int main() {
  int N;
  cin >> N;
  g.resize(N);
  vector<int> a(N - 1), b(N - 1);
  for(int i = 0;i < N - 1;i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }
  dfs(0, -1);
  dist.resize(N, -1);

  queue<int> que;
  dist[last] = 0;
  que.push(last);

  while(!que.empty()) {
    int v = que.front();
    que.pop();
    for(int t : g[v]) {
      if(dist[t] == -1) {
        dist[t] = dist[v] + 1;
        que.push(t);
      }
    }
  }

  int vv = *max_element(begin(dist), end(dist));
  vv++;
  cout << N - vv << endl;
}
0