結果
問題 |
No.3113 The farthest point
|
ユーザー |
![]() |
提出日時 | 2025-04-19 13:19:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 166 ms / 2,000 ms |
コード長 | 1,337 bytes |
コンパイル時間 | 1,157 ms |
コンパイル使用メモリ | 78,496 KB |
実行使用メモリ | 18,184 KB |
最終ジャッジ日時 | 2025-04-19 13:19:44 |
合計ジャッジ時間 | 5,624 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; struct Edge { int to; ll weight; }; vector<vector<Edge>> adj; ll ans = 0; ll dfs(int u, int p) { ll max1 = 0; ll max2 = 0; for (const auto& edge : adj[u]) { int v = edge.to; ll w = edge.weight; if (v == p) continue; ll path_from_v_subtree = dfs(v, u); ll current_path_through_v = 0; if (path_from_v_subtree + w > 0) { current_path_through_v = path_from_v_subtree + w; } if (current_path_through_v >= max1) { max2 = max1; max1 = current_path_through_v; } else if (current_path_through_v > max2) { max2 = current_path_through_v; } } ans = max(ans, max1 + max2); return max1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; adj.resize(n); for (int i = 0; i < n - 1; ++i) { int u_node, v_node; ll w; cin >> u_node >> v_node >> w; --u_node; --v_node; adj[u_node].push_back({v_node, w}); adj[v_node].push_back({u_node, w}); } if (n == 1) { cout << 0 << endl; return 0; } dfs(0, -1); cout << ans << endl; return 0; }