結果
問題 |
No.3113 The farthest point
|
ユーザー |
|
提出日時 | 2025-04-20 19:26:43 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 327 ms / 2,000 ms |
コード長 | 840 bytes |
コンパイル時間 | 2,632 ms |
コンパイル使用メモリ | 196,612 KB |
実行使用メモリ | 25,444 KB |
最終ジャッジ日時 | 2025-04-20 19:26:52 |
合計ジャッジ時間 | 8,612 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MAXN = 500005; vector<pair<int, ll>> G[MAXN]; ll ans = LLONG_MIN; ll dfs(int v, int p) { ll max1 = 0, max2 = 0; // 部分木内の最大と2番目のパス長 for (auto [to, w] : G[v]) { if (to == p) continue; ll d = dfs(to, v) + w; if (d > max1) { max2 = max1; max1 = d; } else if (d > max2) { max2 = d; } } ans = max(ans, max1 + max2); // 途中で一番長いパスを更新 return max1; } int main() { int N; cin >> N; for (int i = 0; i < N - 1; ++i) { int u, v; ll w; cin >> u >> v >> w; G[u].emplace_back(v, w); G[v].emplace_back(u, w); } dfs(1, -1); cout << ans << endl; return 0; }