結果

問題 No.806 木を道に
ユーザー hipopo
提出日時 2020-01-28 18:47:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,326 bytes
コンパイル時間 1,355 ms
コンパイル使用メモリ 116,752 KB
最終ジャッジ日時 2025-01-08 20:42:18
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 9 WA * 18
権限があれば一括ダウンロードができます

ソースコード

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