結果

問題 No.912 赤黒木
ユーザー たぴちゃんたぴちゃん
提出日時 2019-10-17 02:47:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 336 ms / 3,000 ms
コード長 923 bytes
コンパイル時間 1,933 ms
コンパイル使用メモリ 201,560 KB
実行使用メモリ 35,252 KB
最終ジャッジ日時 2023-09-07 20:35:49
合計ジャッジ時間 10,178 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,264 KB
testcase_01 AC 4 ms
9,104 KB
testcase_02 AC 5 ms
9,136 KB
testcase_03 AC 5 ms
9,220 KB
testcase_04 AC 5 ms
9,132 KB
testcase_05 AC 5 ms
9,228 KB
testcase_06 AC 4 ms
9,416 KB
testcase_07 AC 4 ms
9,208 KB
testcase_08 AC 4 ms
9,176 KB
testcase_09 AC 5 ms
9,116 KB
testcase_10 AC 4 ms
9,304 KB
testcase_11 AC 5 ms
9,236 KB
testcase_12 AC 321 ms
17,316 KB
testcase_13 AC 332 ms
17,536 KB
testcase_14 AC 308 ms
17,276 KB
testcase_15 AC 336 ms
17,700 KB
testcase_16 AC 327 ms
17,380 KB
testcase_17 AC 329 ms
17,308 KB
testcase_18 AC 317 ms
17,100 KB
testcase_19 AC 305 ms
17,052 KB
testcase_20 AC 304 ms
16,988 KB
testcase_21 AC 301 ms
17,148 KB
testcase_22 AC 280 ms
15,920 KB
testcase_23 AC 281 ms
15,868 KB
testcase_24 AC 293 ms
15,936 KB
testcase_25 AC 322 ms
17,356 KB
testcase_26 AC 335 ms
17,456 KB
testcase_27 AC 309 ms
17,400 KB
testcase_28 AC 333 ms
35,236 KB
testcase_29 AC 336 ms
35,132 KB
testcase_30 AC 334 ms
35,252 KB
testcase_31 AC 309 ms
33,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;

int N;
vector<int> G[200000];
int deg[200000];
int dp[200000][3];

void calc(int v, int p){
    for(auto to : G[v]){
        if(to == p) continue;
        calc(to, v);
        deg[v] += deg[to];
        int nxt[3] = {INF, INF, INF};
        for(int i = 0; i < 3; i++){
            for(int j = 0; i + j < 3; j++){
                nxt[i + j] = min(nxt[i + j], dp[v][i] + dp[to][j] + !(deg[to] % 2 == j % 2));
            }
        }
        swap(dp[v], nxt);
    }
}

int main(){
    cin >> N;
    for(int i = 0; i < N - 1; i++){
        int A, B;
        cin >> A >> B;
        A--; B--;
        G[A].push_back(B);
        G[B].push_back(A);
    }
    for(int i = 0; i < N - 1; i++){
        int C, D;
        cin >> C >> D;
        C--; D--;
        deg[C]++;
        deg[D]++;
    }
    calc(0, -1);
    cout << min(dp[0][0], dp[0][2]) + N - 1 << endl;
}
0