結果

問題 No.806 木を道に
ユーザー xuzijian629xuzijian629
提出日時 2019-03-22 21:40:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 702 bytes
コンパイル時間 2,197 ms
コンパイル使用メモリ 207,392 KB
実行使用メモリ 14,156 KB
最終ジャッジ日時 2023-10-19 06:39:03
合計ジャッジ時間 4,365 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 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 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 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 43 ms
10,476 KB
testcase_25 AC 65 ms
14,156 KB
testcase_26 AC 21 ms
5,232 KB
testcase_27 AC 67 ms
9,388 KB
testcase_28 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    int n;
    cin >> n;
    vector<vector<int>> adj(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    vector<int> ds(n);
    function<void(int, int, int)> dfs = [&](int v, int p, int d) {
        ds[v] = d;
        for (int s : adj[v]) {
            if (s != p) {
                dfs(s, v, d + 1);
            }
        }
    };
    dfs(0, -1, 0);
    int k = max_element(ds.begin(), ds.end()) - ds.begin();
    dfs(k, -1, 0);
    int diameter = *max_element(ds.begin(), ds.end());
    cout << n - 1 - diameter << endl;
}
0