結果

問題 No.3113 The farthest point
ユーザー V_Melville
提出日時 2025-04-19 01:05:17
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 840 bytes
コンパイル時間 4,666 ms
コンパイル使用メモリ 351,212 KB
実行使用メモリ 15,188 KB
最終ジャッジ日時 2025-04-19 01:05:28
合計ジャッジ時間 9,540 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

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

struct Edge {
    int to, co;
    Edge() {}
    Edge(int to, int co): to(to), co(co) {}
};

int main() {
    int n;
    cin >> n;
    
    vector<vector<Edge>> g(n);
    rep(i, n-1) {
        int a, b, c;
        cin >> a >> b >> c;
        --a; --b;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    
    auto dfs = [&](auto f, int v, ll d=0, int p=-1) -> P {
        P res(d, v);
        if (p == -1) res = P(-1e18, -1);
        for (auto [u, w] : g[v]) if (u != p) {
            res = max(res, f(f, u, max(0ll, d+w), v));
        }
        return res;
    };
    int a = dfs(dfs, 0).second;
    ll ans = dfs(dfs, a).first;
    cout << ans << '\n';
    
    return 0;
}
0