結果

問題 No.1976 Cut then Connect
ユーザー SSRSSSRS
提出日時 2022-06-10 22:03:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,269 bytes
コンパイル時間 2,132 ms
コンパイル使用メモリ 182,808 KB
実行使用メモリ 15,068 KB
最終ジャッジ日時 2023-10-21 05:11:29
合計ジャッジ時間 5,330 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
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 -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
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 WA -
testcase_28 AC 2 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 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  queue<int> Q;
  vector<int> bfs;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    bfs.push_back(v);
    for (int w : E[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  reverse(bfs.begin(), bfs.end());
  vector<int> dp1(N, 0);
  vector<int> D1(N, 0);
  for (int v : bfs){
    vector<int> A;
    for (int w : c[v]){
      dp1[v] = max(dp1[v], dp1[w] + 1);
      D1[v] = max(D1[v], D1[w]);
      A.push_back(dp1[w] + 1);
    }
    sort(A.begin(), A.end(), greater<int>());
    if (A.size() == 1){
      D1[v] = max(D1[v], A[0]);
    }
    if (A.size() >= 2){
      D1[v] = max(D1[v], A[0] + A[1]);
    }
  }
  reverse(bfs.begin(), bfs.end());
  vector<int> dp2(N, 0);
  vector<int> D2(N, 0);
  for (int v : bfs){
    int cnt = c[v].size();
    vector<int> L1(cnt + 1, 0), L2(cnt + 1, 0), L(cnt + 1, 0);
    for (int i = 0; i < cnt; i++){
      int w = c[v][i];
      L1[i + 1] = max(L1[i], dp1[w] + 1);
      L2[i + 1] = max(L2[i], min(L1[i], dp1[w] + 1));
      L[i + 1] = max(L[i], D1[w]);
    }
    vector<int> R1(cnt + 1, 0), R2(cnt + 1, 0), R(cnt + 1, 0);
    for (int i = cnt - 1; i >= 0; i--){
      int w = c[v][i];
      R1[i] = max(R1[i + 1], dp1[w] + 1);
      R2[i] = max(R2[i + 1], min(R1[i + 1], dp1[w] + 1));
      R[i] = max(R[i + 1], D1[w]);
    }
    for (int i = 0; i < cnt; i++){
      int w = c[v][i];
      dp2[w] = max(L1[i] + 1, R1[i + 1] + 1);
      if (v != 0){
        dp2[w] = max(dp2[w], dp2[v] + 1);
      }
      D2[w] = max(L[i], R[i]);
      D2[w] = max(D2[w], L1[i] + L2[i]);
      D2[w] = max(D2[w], R1[i + 1] + R2[i + 1]);
      D2[w] = max(D2[w], L1[i] + R1[i + 1]);
      if (v != 0){
        D2[w] = max(D2[w], dp2[v] + L1[i]);
        D2[w] = max(D2[w], dp2[v] + R1[i + 1]);
      }
    }
  }
  int ans = N;
  for (int i = 1; i < N; i++){
    ans = min(ans, max({D1[i], D2[i], (D1[i] + 1) / 2 + (D2[i] + 1) / 2 + 1}));
  }
  cout << ans << endl;
}
0