結果

問題 No.3113 The farthest point
ユーザー Yu_212
提出日時 2025-04-19 08:52:05
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,302 bytes
コンパイル時間 3,502 ms
コンパイル使用メモリ 284,360 KB
実行使用メモリ 19,872 KB
最終ジャッジ日時 2025-04-19 08:52:14
合計ジャッジ時間 7,845 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int iinf = 1e9;
const ll inf = 1e18;

template<typename T>
ostream& operator<<(ostream &o, vector<T> v) {
    for (int i = 0; i < v.size(); i++)
        o << v[i] << (i+1<v.size()?" ":"");
    return o;
}

struct Edge { int to; ll cost; };

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int n;
    cin >> n;
    vector<vector<Edge>> g(n);
    for (int i = 0; i < n-1; i++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        u--, v--;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    auto farthest = [&](int src){
        vector<ll> dist(n+1, -1);
        queue<int> q;
        dist[src] = 0;
        q.push(src);
        int far_node = src;
        while(!q.empty()){
            int v = q.front(); q.pop();
            for(auto [u, w] : g[v]){
                if(dist[u] == -1){
                    dist[u] = dist[v] + w;
                    q.push(u);
                    if(dist[u] > dist[far_node]){
                        far_node = u;
                    }
                }
            }
        }
        return pair<int,ll>(far_node, dist[far_node]);
    };

    auto [x, _] = farthest(0);
    auto [y, ans] = farthest(x);
    cout << ans << endl;
    return 0;
}
0