結果
| 問題 |
No.3113 The farthest point
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-06 21:13:46 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 252 ms / 2,000 ms |
| コード長 | 1,228 bytes |
| コンパイル時間 | 1,461 ms |
| コンパイル使用メモリ | 112,996 KB |
| 実行使用メモリ | 18,000 KB |
| 最終ジャッジ日時 | 2025-04-14 22:28:46 |
| 合計ジャッジ時間 | 6,375 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
int n;
std::cin >> n;
std::vector adj(n, std::vector<std::pair<long long, int>>());
for(int i = 0; i < n-1; i++) {
int u, v, w;
std::cin >> u >> v >> w;
u--; v--;
adj[u].emplace_back(w, v);
adj[v].emplace_back(w, u);
}
auto dfs = [&](auto&& self, int v, int p) -> std::pair<long long, long long> {
// {自分を端に含むもの, 含まないもの}
std::pair<long long, long long> res = {0, 0};
std::vector<long long> children_score;
for(auto [w, u] : adj[v]) {
if(u == p) continue;
auto [contain, not_contain] = self(self, u, v);
res.first = std::max(res.first, contain + w);
children_score.push_back(contain + w);
res.second = std::max(res.second, not_contain);
}
std::sort(children_score.rbegin(), children_score.rend());
if(children_score.size() >= 1)
res.second = std::max(res.second, children_score[0]);
if(children_score.size() >= 2)
res.second = std::max(res.second, children_score[0] + children_score[1]);
return res;
};
auto [contain, not_contain] = dfs(dfs, 0, -1);
std::cout << std::max(contain, not_contain) << std::endl;
}