結果

問題 No.806 木を道に
ユーザー hipopohipopo
提出日時 2020-01-28 18:47:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,326 bytes
コンパイル時間 1,131 ms
コンパイル使用メモリ 118,092 KB
実行使用メモリ 14,908 KB
最終ジャッジ日時 2023-10-13 20:50:32
合計ジャッジ時間 3,456 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 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 40 ms
11,020 KB
testcase_25 AC 61 ms
14,908 KB
testcase_26 AC 20 ms
4,880 KB
testcase_27 AC 62 ms
8,904 KB
testcase_28 AC 3 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)

using ll = long long;
using P = pair<int, int>;

const long long MOD = 1e9+7;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T>
istream& operator>>(istream &is, vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i);
    return is;
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> graph(n);
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        graph.at(a).push_back(b);
        graph.at(b).push_back(a);
    }

    // first 直径, second 最遠頂点
    function<P(int, int)> dfs = [&](int p, int v){
        P res(0, v);
        for (int to: graph.at(v)) if (to != p) {
            P r = dfs(v, to);
            r.first++;
            if (res.first < r.first) res = r;
        }
        return res;
    };

    P x = dfs(-1, 0);
    P y = dfs(-1, x.second);

    cout << n - 1 - y.first << endl;
}
0