結果

問題 No.912 赤黒木
ユーザー ei1333333ei1333333
提出日時 2019-08-20 23:27:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,136 bytes
コンパイル時間 4,963 ms
コンパイル使用メモリ 200,212 KB
実行使用メモリ 50,044 KB
最終ジャッジ日時 2023-09-07 20:34:35
合計ジャッジ時間 11,515 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,696 KB
testcase_01 AC 3 ms
8,764 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 AC 298 ms
17,292 KB
testcase_23 AC 299 ms
17,332 KB
testcase_24 AC 298 ms
17,332 KB
testcase_25 AC 367 ms
16,876 KB
testcase_26 AC 376 ms
17,064 KB
testcase_27 AC 359 ms
16,768 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 421 ms
50,044 KB
testcase_31 AC 394 ms
47,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int inf = 1 << 30;

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