結果

問題 No.3113 The farthest point
ユーザー 入野拳樹
提出日時 2025-04-20 00:15:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 893 bytes
コンパイル時間 660 ms
コンパイル使用メモリ 80,776 KB
実行使用メモリ 18,228 KB
最終ジャッジ日時 2025-04-20 00:15:47
合計ジャッジ時間 6,198 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>

using namespace std;
using ll = long long;

vector<vector<pair<int, ll>>> tree;
vector<bool> visited;

pair<int, ll> dfs(int node, int parent) {
    pair<int, ll> res = {node, 0};
    for (auto [to, weight] : tree[node]) {
        if (to == parent) continue;
        auto [far_node, dist] = dfs(to, node);
        dist += weight;
        if (dist > res.second) {
            res = {far_node, dist};
        }
    }
    return res;
}

int main() {
    int N;
    cin >> N;

    tree.resize(N + 1);

    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        tree[u].emplace_back(v, w);
        tree[v].emplace_back(u, w);
    }

    auto [farthest_node, d] = dfs(1, -1);

    auto [nv, max_dist] = dfs(farthest_node, -1);

    cout << max_dist << endl;

    return 0;
}
0