結果

問題 No.1976 Cut then Connect
ユーザー 👑 emthrmemthrm
提出日時 2023-07-23 05:40:12
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 3,427 bytes
コンパイル時間 3,589 ms
コンパイル使用メモリ 281,428 KB
実行使用メモリ 15,300 KB
最終ジャッジ日時 2023-10-24 12:23:56
合計ジャッジ時間 6,763 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 78 ms
14,772 KB
testcase_03 AC 38 ms
10,020 KB
testcase_04 AC 14 ms
5,532 KB
testcase_05 AC 33 ms
8,964 KB
testcase_06 AC 58 ms
12,132 KB
testcase_07 AC 35 ms
9,228 KB
testcase_08 AC 80 ms
15,300 KB
testcase_09 AC 59 ms
13,452 KB
testcase_10 AC 16 ms
6,060 KB
testcase_11 AC 73 ms
15,036 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 54 ms
12,132 KB
testcase_14 AC 52 ms
11,604 KB
testcase_15 AC 48 ms
11,076 KB
testcase_16 AC 64 ms
13,980 KB
testcase_17 AC 23 ms
7,380 KB
testcase_18 AC 6 ms
4,348 KB
testcase_19 AC 55 ms
12,132 KB
testcase_20 AC 79 ms
15,300 KB
testcase_21 AC 43 ms
10,284 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n; cin >> n;
  vector<vector<int>> graph(n);
  REP(_, n - 1) {
    int u, v; cin >> u >> v; --u; --v;
    graph[u].emplace_back(v);
    graph[v].emplace_back(u);
  }
  vector<vector<tuple<int, int, int>>> dp(n);
  const auto dfs1 = [&](auto dfs1, const int par, const int ver) -> void {
    if (par != -1) graph[ver].erase(ranges::find(graph[ver], par));
    for (const int e : graph[ver]) {
      dfs1(dfs1, ver, e);
      int diam = 0;
      for (const int d : dp[e] | views::elements<1>) chmax(diam, d);
      if (!dp[e].empty()) chmax(diam, get<0>(dp[e].front()));
      if (dp[e].size() >= 2) chmax(diam, get<0>(dp[e][0]) + get<0>(dp[e][1]));
      dp[ver].emplace_back(dp[e].empty() ? 1 : get<0>(dp[e].front()) + 1, diam, e);
    }
    if (!dp[ver].empty()) {
      ranges::sort(dp[ver], greater<int>(), [](const tuple<int, int, int>& x) -> int { return get<0>(x); });
    }
  };
  dfs1(dfs1, -1, 0);
  // REP(i, n) {
  //   cout << i + 1 << ':';
  //   for (const auto& [root, diam, e] : dp[i]) {
  //     cout << " (" << root << ',' << diam << ',' << e + 1 << ')';
  //   }
  //   cout << '\n';
  // }
  int ans = INF;
  const auto dfs2 = [&](auto dfs2, const int par, const int ver) -> void {
    ranges::sort(dp[ver], greater<int>(), [](const tuple<int, int, int>& x) -> int { return get<0>(x); });
    const int ch = dp[ver].size();
    vector<int> from_l(ch, 0), from_r(ch, 0);
    FOR(i, 1, ch) from_l[i] = max(from_l[i - 1], get<1>(dp[ver][i - 1]));
    for (int i = ch - 2; i >= 0; --i) {
      from_r[i] = max(from_r[i + 1], get<1>(dp[ver][i + 1]));
    }
    REP(i, ch) {
      if (get<2>(dp[ver][i]) == par) continue;
      const int through = [&]() -> int {
        if (ch == 1) return 0;
        if (ch == 2) return get<0>(dp[ver][i == 0 ? 1 : 0]);
        if (i == 0) return get<0>(dp[ver][1]) + get<0>(dp[ver][2]);
        if (i == 1) return get<0>(dp[ver][0]) + get<0>(dp[ver][2]);
        return get<0>(dp[ver][0]) + get<0>(dp[ver][1]);
      } ();
      const int x = max({from_l[i], from_r[i], through}), y = get<1>(dp[ver][i]);
      chmin(ans, max({x, y, (x + 1) / 2 + (y + 1) / 2 + 1}));
      dp[get<2>(dp[ver][i])].emplace_back(ch == 1 ? 1 : get<0>(dp[ver][i == 0 ? 1 : 0]) + 1, x, ver);
      dfs2(dfs2, ver, get<2>(dp[ver][i]));
    }
  };
  dfs2(dfs2, -1, 0);
  // REP(i, n) {
  //   cout << i + 1 << ':';
  //   for (const auto& [root, diam, e] : dp[i]) {
  //     cout << " (" << root << ',' << diam << ',' << e + 1 << ')';
  //   }
  //   cout << '\n';
  // }
  cout << ans << '\n';
  return 0;
}
0