結果

問題 No.912 赤黒木
ユーザー ei1333333ei1333333
提出日時 2019-09-07 20:26:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 411 ms / 3,000 ms
コード長 1,141 bytes
コンパイル時間 2,239 ms
コンパイル使用メモリ 202,908 KB
実行使用メモリ 49,972 KB
最終ジャッジ日時 2023-09-07 20:33:21
合計ジャッジ時間 11,498 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,704 KB
testcase_01 AC 4 ms
8,676 KB
testcase_02 AC 4 ms
8,744 KB
testcase_03 AC 5 ms
8,724 KB
testcase_04 AC 4 ms
8,716 KB
testcase_05 AC 5 ms
8,728 KB
testcase_06 AC 4 ms
8,732 KB
testcase_07 AC 4 ms
8,808 KB
testcase_08 AC 4 ms
8,856 KB
testcase_09 AC 4 ms
8,704 KB
testcase_10 AC 4 ms
8,768 KB
testcase_11 AC 4 ms
8,820 KB
testcase_12 AC 354 ms
16,964 KB
testcase_13 AC 365 ms
17,052 KB
testcase_14 AC 341 ms
16,632 KB
testcase_15 AC 372 ms
17,060 KB
testcase_16 AC 362 ms
16,992 KB
testcase_17 AC 367 ms
16,896 KB
testcase_18 AC 353 ms
16,752 KB
testcase_19 AC 347 ms
16,616 KB
testcase_20 AC 330 ms
16,788 KB
testcase_21 AC 342 ms
16,456 KB
testcase_22 AC 300 ms
17,460 KB
testcase_23 AC 307 ms
17,512 KB
testcase_24 AC 301 ms
17,316 KB
testcase_25 AC 359 ms
16,876 KB
testcase_26 AC 373 ms
17,132 KB
testcase_27 AC 356 ms
16,780 KB
testcase_28 AC 411 ms
49,972 KB
testcase_29 AC 400 ms
49,736 KB
testcase_30 AC 392 ms
49,916 KB
testcase_31 AC 381 ms
47,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int inf = (1 << 29) - 1;

int N;
vector< int > g[200000];
bool deg[200000], sum[200000];
int dp[200000][3];

void predfs(int idx, int par) {
  sum[idx] = deg[idx];
  for(auto &to : g[idx]) {
    if(to == par) continue;
    predfs(to, idx);
    sum[idx] ^= sum[to];
  }
}

void dfs(int idx, int par) {
  vector< int > subdp{0, deg[idx] ? 0 : inf, inf};
  for(auto &to : g[idx]) {
    if(to == par) continue;
    dfs(to, idx);
    vector< int > nxtdp{inf, inf, inf};
    for(int i = 0; i < 3; i++) {
      for(int j = 0; i + j < 3; j++) {
        nxtdp[i + j] = min(nxtdp[i + j], subdp[i] + dp[to][j] + (j + sum[to]) % 2);
      }
    }
    subdp.swap(nxtdp);
  }
  for(int i = 0; i < 3; i++) {
    dp[idx][i] = subdp[i];
  }
}

int main() {
  cin >> N;
  for(int i = 1; i < N; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  for(int i = 1; i < N; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    deg[a] ^= 1;
    deg[b] ^= 1;
  }
  predfs(0, -1);
  dfs(0, -1);
  cout << min(dp[0][0], dp[0][2]) + (N - 1) << endl;
}
0