結果

問題 No.806 木を道に
ユーザー pekempeypekempey
提出日時 2019-03-22 21:26:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 601 bytes
コンパイル時間 815 ms
コンパイル使用メモリ 77,768 KB
実行使用メモリ 21,308 KB
最終ジャッジ日時 2023-10-19 06:24:35
合計ジャッジ時間 3,024 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,004 KB
testcase_01 AC 3 ms
6,004 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 3 ms
6,008 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
6,008 KB
testcase_08 AC 3 ms
6,008 KB
testcase_09 AC 3 ms
6,008 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 49 ms
16,088 KB
testcase_25 AC 73 ms
21,308 KB
testcase_26 AC 22 ms
7,092 KB
testcase_27 AC 71 ms
9,956 KB
testcase_28 AC 5 ms
6,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

vector<int> G[100000];
int dp[100000];
int ans;

void dfs(int u, int p) {
  vector<int> a = {0, 0};
  for (int v : G[u]) if (v != p) {
    dfs(v, u);
    dp[u] = max(dp[u], dp[v] + 1);
    a.push_back(dp[v] + 1);
  }
  sort(a.rbegin(), a.rend());
  ans = max(ans, a[0] + a[1]);
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(0, -1);
  cout << N - (ans + 1) << '\n';
}
0