結果

問題 No.3113 The farthest point
ユーザー V_Melville
提出日時 2025-04-19 01:00:29
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 828 bytes
コンパイル時間 3,398 ms
コンパイル使用メモリ 281,344 KB
実行使用メモリ 18,128 KB
最終ジャッジ日時 2025-04-19 01:00:39
合計ジャッジ時間 5,869 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

signed main() {
    cin.tie(nullptr) -> sync_with_stdio(false);
    
    int n;
    cin >> n;
    
    vector<vector<P>> 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, int d=0, int p=-1) -> P {
        P res(d, v);
        if (p == -1) res = P(0, -1);
        for (auto [u, w] : g[v]) {
            if (u == p) continue;
            res = max(res, f(f, u, max(0ll, d+w), v));
        }
        return res;
    };
    
    int a = dfs(dfs, 0).second;
    int ans = dfs(dfs, a).first;
    cout << ans << '\n';
    
    return 0;
}
0