結果

問題 No.2504 NOT Path Painting
ユーザー 👑 emthrmemthrm
提出日時 2023-08-12 19:28:47
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,741 bytes
コンパイル時間 1,178 ms
コンパイル使用メモリ 97,116 KB
実行使用メモリ 11,252 KB
最終ジャッジ日時 2023-08-20 01:14:30
合計ジャッジ時間 5,820 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <tuple>
#include <utility>
#include <vector>

namespace emthrm {

std::pair<int, std::vector<int>> double_sweep(
    const std::vector<std::vector<int>>& graph) {
  const auto dfs1 = [&graph](auto dfs1, const int par, const int ver)
      -> std::pair<int, int> {
    std::pair<int, int> res{0, ver};
    for (const int e : graph[ver]) {
      if (e != par) {
        std::pair<int, int> child = dfs1(dfs1, ver, e);
        ++child.first;
        if (child.first > res.first) res = child;
      }
    }
    return res;
  };
  const int s = dfs1(dfs1, -1, 0).second;
  const auto [diameter, t] = dfs1(dfs1, -1, s);
  std::vector<int> path{s};
  const auto dfs2 = [&graph, t, &path](auto dfs2, const int par, const int ver)
      -> bool {
    if (ver == t) return true;
    for (const int e : graph[ver]) {
      if (e != par) {
        path.emplace_back(e);
        if (dfs2(dfs2, ver, e)) return true;
        path.pop_back();
      }
    }
    return false;
  };
  assert(dfs2(dfs2, -1, s));
  return {diameter, path};
}

}  // namespace emthrm

// 【テストケースのチェック】
// 木の直径を標準エラー出力に出力する。
int main() {
  constexpr int kMaxT = 100000, kMaxN = 40000;

  int t;
  std::cin >> t;
  assert(1 <= t && t <= kMaxT);

  while (t--) {
    int n;
    std::cin >> n;
    assert(2 <= n && n <= kMaxN);
    std::vector<std::vector<int>> tree(n);
    for (int i = 0; i < n - 1; ++i) {
      int u, v;
      std::cin >> u >> v;
      assert(1 <= u && u <= n && 1 <= v && v <= n);
      --u; --v;
      tree[u].emplace_back(v);
      tree[v].emplace_back(u);
    }
    std::cerr << emthrm::double_sweep(tree).second.size() << '\n';
  }
  return 0;
}
0