結果

問題 No.3113 The farthest point
ユーザー srjywrdnprkt
提出日時 2025-08-26 02:01:48
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,173 bytes
コンパイル時間 4,058 ms
コンパイル使用メモリ 290,156 KB
実行使用メモリ 18,048 KB
最終ジャッジ日時 2025-08-26 02:01:59
合計ジャッジ時間 8,978 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/modint>

using namespace std;
//using namespace atcoder;
using ll = long long;
//using mint = modint998244353;

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    /*
       木DP
       dp(i) = 頂点iを末端とする重み最大のパス
       これと、各頂点iをLCAとするパスが候補。
    */

    ll N, ans=0;
    cin >> N;
    vector<vector<pair<ll, ll>>> E(N);
    for (int i=0; i<N-1; i++){
        ll u, v, c;
        cin >> u >> v >> c;
        u--; v--;
        E[u].push_back({v, c});
        E[v].push_back({u, c});
    }
    auto dfs=[&](auto self, int from, int p)->ll{
        vector<ll> v;
        ll res=0;
        for (auto [to, c] : E[from]){
            if (to == p) continue;
            ll x = self(self, to, from);
            v.push_back(x+c);
        }
        sort(v.rbegin(), v.rend());
        if (v.size() >= 1){
            ans = max(ans, v[0]);
            res = max(res, v[0]);
        }
        if (v.size() >= 2){
            ans = max(ans, v[0]+v[1]);
        }
        return res;
    };
    dfs(dfs, 0, -1);
    cout << ans << endl;

    return 0;
}
0