結果
| 問題 | No.3113 The farthest point |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-19 11:54:36 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 270 ms / 2,000 ms |
| コード長 | 1,012 bytes |
| 記録 | |
| コンパイル時間 | 855 ms |
| コンパイル使用メモリ | 78,556 KB |
| 実行使用メモリ | 18,060 KB |
| 最終ジャッジ日時 | 2025-04-19 11:54:43 |
| 合計ジャッジ時間 | 6,677 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll NEG_INF = -1e18;
struct Edge {
int to;
ll cost;
};
int N;
vector<vector<Edge>> tree;
ll answer = NEG_INF;
ll dfs(int node, int parent) {
ll max1 = 0, max2 = 0;
for (const auto& edge : tree[node]) {
if (edge.to == parent) continue;
ll childLen = dfs(edge.to, node) + edge.cost;
if (childLen > max1) {
max2 = max1;
max1 = childLen;
} else if (childLen > max2) {
max2 = childLen;
}
}
// 更新:このノードを通る直径候補
answer = max(answer, max1 + max2);
// このノードから上へ渡す最大長
return max1;
}
int main() {
cin >> N;
tree.resize(N);
for (int i = 0; i < N - 1; ++i) {
int a, b;
ll cost;
cin >> a >> b >> cost;
--a;
--b; // 1-indexed -> 0-indexed
tree[a].push_back({b, cost});
tree[b].push_back({a, cost});
}
dfs(0, -1);
cout << answer << endl;
return 0;
}