結果

問題 No.3113 The farthest point
ユーザー shibh308
提出日時 2025-04-18 22:12:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 836 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 201,220 KB
実行使用メモリ 16,856 KB
最終ジャッジ日時 2025-04-18 22:12:28
合計ジャッジ時間 6,976 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main(){
    int n;
    cin >> n;
    vector<vector<pair<int,int>>> edges(n);
    for(int i = 0; i < n - 1; ++i){
        int u, v, w;
        cin >> u >> v >> w;
        --u, --v;
        edges[u].emplace_back(v, w);
        edges[v].emplace_back(u, w);
    }
    vector<i64> dist(n, 1e18);
    auto dfs = [&](int u, i64 d, auto&&dfs) -> void {
        dist[u] = d;
        for(auto [v, w] : edges[u]){
            if(dist[v] == 1e18){
                dfs(v, d + w, dfs);
            }
        }
    };
    dfs(0, 0, dfs);
    int farthest = max_element(dist.begin(), dist.end()) - dist.begin();
    dist.assign(n, 1e18);
    dfs(farthest, 0, dfs);
    int farthest2 = max_element(dist.begin(), dist.end()) - dist.begin();
    cout << dist[farthest2] << '\n';
}
0