結果

問題 No.3113 The farthest point
ユーザー みどりむし🦠
提出日時 2025-04-19 13:17:14
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 873 bytes
コンパイル時間 3,788 ms
コンパイル使用メモリ 279,856 KB
実行使用メモリ 18,084 KB
最終ジャッジ日時 2025-04-19 13:17:25
合計ジャッジ時間 10,288 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
struct Edge {
    int to;
    T cost;
};
using Graph = vector<vector<Edge<long long>>>; 
template <typename T>
pair<T, int> dfs(const Graph &G, int u, int par) { 
    pair<T, int> ret = make_pair((T)0, u);
    for (auto e : G[u]) {
        if (e.to == par) continue;
        auto next = dfs<T>(G, e.to, u);
        next.first += e.cost;
        ret = max(ret, next);
    }
    return ret;
}
template <typename T>
T tree_diamiter(const Graph &G) {
    pair<T, int> p = dfs<T>(G, 0, -1);
    pair<T, int> q = dfs<T>(G, p.second, -1);
    return q.first;
}

int main() {
	int n; std::cin >> n;
	Graph G(n);

	for(int i : std::views::iota(0, n - 1)) {
		int u, v, w; std::cin >> u >> v >> w; --u, --v;
		G[u].emplace_back(v, w);
		G[v].emplace_back(u, w);
	}

	std::cout << tree_diamiter<long>(G) << "\n";
}
0