結果

問題 No.3113 The farthest point
ユーザー しとりん
提出日時 2025-04-20 13:38:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 967 bytes
コンパイル時間 2,389 ms
コンパイル使用メモリ 207,216 KB
実行使用メモリ 18,212 KB
最終ジャッジ日時 2025-04-20 13:38:28
合計ジャッジ時間 6,492 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll ans = 0;

int N;
vector<vector<pair<int,ll>>> adj;

ll dfs(int v, int par) {
    vector<ll> child_vals;
    for (auto [u, w] : adj[v]) {
        if (u == par) continue;
        ll down = dfs(u, v) + w;
        child_vals.push_back(max(down, w));
    }
    if (child_vals.empty()) {
        return LLONG_MIN / 2;
    }
    sort(child_vals.rbegin(), child_vals.rend());
    // 1本分
    ans = max(ans, child_vals[0]);
    // 2本分
    if (child_vals.size() >= 2) {
        ans = max(ans, child_vals[0] + child_vals[1]);
    }
    return child_vals[0];
}

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

    cin >> N;
    adj.assign(N+1, {});
    for(int i = 1; i < N; i++){
        int u, v; ll w;
        cin >> u >> v >> w;
        adj[u].emplace_back(v, w);
        adj[v].emplace_back(u, w);
    }
    dfs(1, -1);
    cout << max(ans, 0LL) << "\n";
    return 0;
}
0