結果

問題 No.912 赤黒木
ユーザー Iván SotoIván Soto
提出日時 2021-05-12 19:11:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 491 ms / 3,000 ms
コード長 3,096 bytes
コンパイル時間 2,496 ms
コンパイル使用メモリ 215,396 KB
実行使用メモリ 106,492 KB
最終ジャッジ日時 2023-10-24 12:46:18
合計ジャッジ時間 14,832 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 411 ms
64,516 KB
testcase_13 AC 426 ms
66,100 KB
testcase_14 AC 397 ms
62,208 KB
testcase_15 AC 426 ms
66,628 KB
testcase_16 AC 406 ms
64,252 KB
testcase_17 AC 408 ms
64,780 KB
testcase_18 AC 391 ms
62,472 KB
testcase_19 AC 396 ms
61,416 KB
testcase_20 AC 390 ms
60,888 KB
testcase_21 AC 381 ms
60,360 KB
testcase_22 AC 393 ms
66,960 KB
testcase_23 AC 393 ms
67,232 KB
testcase_24 AC 395 ms
67,232 KB
testcase_25 AC 411 ms
64,516 KB
testcase_26 AC 425 ms
66,364 KB
testcase_27 AC 399 ms
63,460 KB
testcase_28 AC 484 ms
106,492 KB
testcase_29 AC 483 ms
105,964 KB
testcase_30 AC 491 ms
106,492 KB
testcase_31 AC 460 ms
100,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *  author: ivanzuki   
 *  created: Wed May 12 2021
**/
#include <bits/stdc++.h>

using namespace std;

const int inf = (int) 1e9;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  vector<int> deg(n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    ++deg[x];
    ++deg[y];
  }
  vector<vector<vector<int>>> dp(n, vector<vector<int>>(4, vector<int>(2, inf)));
  function<void(int, int)> DFS = [&](int v, int pv) {
    dp[v][deg[v] % 2][deg[v] % 2] = 0;
    for (int u : g[v]) {
      if (u == pv) {
        continue;
      }
      DFS(u, v);
      // (# of odd-degree vertices, is v an odd-degree vertex?)
      vector<vector<int>> new_dp(4, vector<int>(2, inf));
      // add the (v, u) edge
      for (int x = 0; x <= 3; x++) {
        for (int y = 0; y < 2; y++) {
          for (int z = 0; z <= 3; z++) {
            for (int w = 0; w < 2; w++) {
              {
                // add the edge
                int p = x + (y == 1 ? -1 : 1) + z + (w == 1 ? -1 : 1);
                int q = 1 - y;
                if ((p >= 0 && p <= 2) || (p == 3 && q == 1)) {
                  new_dp[p][q] = min(new_dp[p][q], 1 + dp[v][x][y] + dp[u][z][w]);
                }
              }
              {
                // do not add the edge
                int p = x + z;
                int q = y;
                if ((p >= 0 && p <= 2) || (p == 3 && q == 1)) {
                  new_dp[p][q] = min(new_dp[p][q], dp[v][x][y] + dp[u][z][w]);
                }
              }
            }
          }
        }
      }
      dp[v] = new_dp;
      // new_dp[0][0] = min(new_dp[0][0], 1 + dp[v][1][1] + dp[u][1][1]);
      // new_dp[1][0] = min(new_dp[1][0], 1 + dp[v][1][1] + dp[u][0][0]);
      // new_dp[1][1] = min(new_dp[1][1], 1 + dp[v][0][0] + dp[u][1][1]);
      // new_dp[2][0] = min(new_dp[2][0], 1 + dp[v][3][1] + dp[u][1][1]);
      // new_dp[2][0] = min(new_dp[2][0], 1 + dp[v][1][1] + dp[u][1][0]);
      // new_dp[2][0] = min(new_dp[2][0], 1 + dp[v][1][1] + dp[u][3][1]);
      // new_dp[2][1] = min(new_dp[2][1], 1 + dp[v][1][0] + dp[u][3][1]);
      // new_dp[2][1] = min(new_dp[2][1], 1 + dp[v][0][0] + dp[u][0][0]);
      // new_dp[2][1] = min(new_dp[2][1], 1 + dp[v][0][0] + dp[u][3][1]);
      // new_dp[2][1] = min(new_dp[2][1], 1 + dp[v][1][0] + dp[u][0][0]);
      // new_dp[3][1] = min(new_dp[3][1], 1 + dp[v][2][0] + dp[u][1][1]);
      // new_dp[3][1] = min(new_dp[3][1], 1 + dp[v][1][0] + dp[u][0][0]);
      // new_dp[3][1] = min(new_dp[3][1], 1 + dp[v][1][0] + dp[u][2][1]);
      // new_dp[3][1] = min(new_dp[3][1], 1 + dp[v][0][0] + dp[u][3][1]);
      // new_dp[3][1] = min(new_dp[3][1], 1 + dp[v][0][0] + dp[u][1][0]);
      // do not add the (v, u) edge
      // new_dp[0][0] = min(new_dp[0][0], 
    }
  };
  DFS(0, -1);
  int ans = n - 1 + min({dp[0][0][0], dp[0][2][0], dp[0][2][1]});
  cout << ans << '\n';
  return 0;
}
0