結果

問題 No.3113 The farthest point
ユーザー sai
提出日時 2025-04-19 05:58:09
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 868 bytes
コンパイル時間 3,253 ms
コンパイル使用メモリ 280,240 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2025-04-19 05:58:17
合計ジャッジ時間 6,682 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  std::cin >> n;
  std::vector<std::vector<std::pair<int, int>>> G(n);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    std::cin >> u >> v >> w;
    u--, v--;
    G[u].emplace_back(v, w);
    G[v].emplace_back(u, w);
  }
  long long ans = 0;
  auto dfs = [&](auto self, int v, int pre) -> long long {
    std::pair<long long, long long> res;
    for (auto [v2, c] : G[v]) {
      if (v2 != pre) {
        long long tmp = self(self, v2, v);
        if (tmp + c > res.second) {
          res.second = tmp + c;
          if (res.second > res.first) {
            std::swap(res.first, res.second);
          }
        }
      }
    }
    ans = std::max(ans, res.first + res.second);
    return res.first;
  };
  dfs(dfs, 0, -1);
  std::cout << ans << '\n';
}
0