結果

問題 No.912 赤黒木
ユーザー pekempeypekempey
提出日時 2019-10-19 00:06:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 328 ms / 3,000 ms
コード長 1,518 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 173,744 KB
実行使用メモリ 74,488 KB
最終ジャッジ日時 2023-09-08 02:58:06
合計ジャッジ時間 9,436 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 288 ms
35,412 KB
testcase_13 AC 295 ms
36,584 KB
testcase_14 AC 268 ms
34,568 KB
testcase_15 AC 276 ms
36,688 KB
testcase_16 AC 271 ms
35,272 KB
testcase_17 AC 271 ms
35,800 KB
testcase_18 AC 250 ms
34,488 KB
testcase_19 AC 250 ms
34,108 KB
testcase_20 AC 245 ms
33,912 KB
testcase_21 AC 245 ms
33,464 KB
testcase_22 AC 220 ms
37,408 KB
testcase_23 AC 219 ms
37,440 KB
testcase_24 AC 215 ms
37,356 KB
testcase_25 AC 265 ms
35,544 KB
testcase_26 AC 270 ms
36,464 KB
testcase_27 AC 241 ms
35,068 KB
testcase_28 AC 328 ms
74,472 KB
testcase_29 AC 320 ms
74,100 KB
testcase_30 AC 321 ms
74,488 KB
testcase_31 AC 308 ms
70,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define repr2(i, l, r) for (int i = (r) - 1; i >= (l); i--)
#define range(a) a.begin(), a.end()

using namespace std;
using ll = long long;

void chmin(int &x, int y) {
  x = min(x, y);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int n; cin >> n;
  vector<vector<int>> g(n);
  rep(i, n - 1) {
    int a, b; cin >> a >> b; a--; b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> deg(n);
  rep(i, n - 1) {
    int c, d; cin >> c >> d; c--; d--;
    deg[c]++;
    deg[d]++;
  }
  int ans = n - 1;
  vector<vector<int>> dp0(n, vector<int>(3, 1e9));
  vector<vector<int>> dp1(n, vector<int>(3, 1e9));
  auto dfs = [&](auto dfs, int u, int p) -> void {
    if (deg[u] % 2 == 0) {
      dp0[u][0] = 0;
    } else {
      dp1[u][0] = 0;
      dp0[u][1] = 0;
    }
    for (int v : g[u]) if (v != p) {
      dfs(dfs, v, u);
      vector<int> tmp0(3, 1e9);
      vector<int> tmp1(3, 1e9);
      rep(i, 3) rep(j, 3) if (i + j <= 2) {
        chmin(tmp0[i + j], dp0[u][i] + dp0[v][j]);
        chmin(tmp0[i + j], dp1[u][i] + dp1[v][j] + 1);
        chmin(tmp1[i + j], dp0[u][i] + dp1[v][j] + 1);
        chmin(tmp1[i + j], dp1[u][i] + dp0[v][j]);
      }
      dp0[u] = tmp0;
      dp1[u] = tmp1;
    }
  };
  dfs(dfs, 0, -1);
  cout << dp0[0][2] + (n - 1) << endl;
}
0