結果

問題 No.3113 The farthest point
ユーザー Rira
提出日時 2025-04-20 14:46:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 902 bytes
コンパイル時間 2,257 ms
コンパイル使用メモリ 197,456 KB
実行使用メモリ 15,640 KB
最終ジャッジ日時 2025-04-20 14:46:50
合計ジャッジ時間 8,837 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int MAX_N = 200000;
vector<pair<int, int>> adj[MAX_N];

pair<int, long long> dfs(int v, int parent) {
    pair<int, long long> res = {v, 0};

    for (auto& edge : adj[v]) {
        int u = edge.first;
        long long w = edge.second;
        if (u != parent) {
            auto next = dfs(u, v);
            next.second += w;
            if (next.second > res.second) {
                res = next;
            }
        }
    }
    
    return res;
}

int main() {
    int N;
    cin >> N;
    
    for (int i = 0; i < N - 1; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    
    pair<int, long long> first = dfs(0, -1);
    
    pair<int, long long> second = dfs(first.first, -1);
    
    cout << second.second << endl;
    
    return 0;
}
0