結果
問題 |
No.3113 The farthest point
|
ユーザー |
|
提出日時 | 2025-04-19 00:47:25 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,224 bytes |
コンパイル時間 | 823 ms |
コンパイル使用メモリ | 78,836 KB |
実行使用メモリ | 32,052 KB |
最終ジャッジ日時 | 2025-04-19 00:47:33 |
合計ジャッジ時間 | 6,388 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 20 WA * 13 |
ソースコード
#include <iostream> #include <vector> using int64 = long long; template<class T> using Graph = std::vector<std::vector<T>>; template<class T> class Edge { public: int to; T weight; Edge(int to, T weight) : to{to}, weight{weight} {} }; template<class Weight> class TreeDiameter { Graph<Edge<Weight>> g; using Result = std::pair<Weight, int64>; Result dfs(int par, int curr) { Result ret(0, curr); for (const auto& [to, weight] : g[curr]) { if (to == par) continue; Result t = dfs(curr, to); t.first += weight; ret = std::max(ret, t); } return ret; } public: TreeDiameter(const Graph<Edge<Weight>>& g) : g{g} {} Weight solve() { Result r = dfs(-1, 0); Result t = dfs(-1, r.second); return t.first; } }; int main() { int N; std::cin >> N; Graph<Edge<int64>> g(N); int64 u, v, w; for (int i = 0; i < N - 1; i++) { std::cin >> u >> v >> w; u--; v--; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } TreeDiameter<int64> tree(g); std::cout << tree.solve() << std::endl; return 0; }