結果

問題 No.912 赤黒木
ユーザー kimiyukikimiyuki
提出日時 2019-10-24 15:04:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 389 ms / 3,000 ms
コード長 1,341 bytes
コンパイル時間 3,218 ms
コンパイル使用メモリ 205,052 KB
実行使用メモリ 55,140 KB
最終ジャッジ日時 2023-09-23 02:14:27
合計ジャッジ時間 15,292 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 344 ms
24,472 KB
testcase_13 AC 360 ms
24,940 KB
testcase_14 AC 336 ms
23,496 KB
testcase_15 AC 382 ms
25,076 KB
testcase_16 AC 356 ms
24,304 KB
testcase_17 AC 353 ms
24,456 KB
testcase_18 AC 385 ms
23,704 KB
testcase_19 AC 343 ms
23,220 KB
testcase_20 AC 326 ms
23,184 KB
testcase_21 AC 334 ms
22,964 KB
testcase_22 AC 324 ms
26,216 KB
testcase_23 AC 322 ms
26,372 KB
testcase_24 AC 329 ms
26,488 KB
testcase_25 AC 351 ms
24,556 KB
testcase_26 AC 364 ms
25,088 KB
testcase_27 AC 375 ms
24,016 KB
testcase_28 AC 389 ms
54,640 KB
testcase_29 AC 381 ms
54,012 KB
testcase_30 AC 366 ms
55,140 KB
testcase_31 AC 342 ms
52,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
using namespace std;
template <class T, class U> inline void chmin(T & a, U const & b) { a = min<T>(a, b); }

int solve(int n, const vector<vector<int> > & g, const vector<vector<int> > & h) {
    const int INF = n;
    function<pair<array<int, 3>, int> (int, int)> go = [&](int x, int parent) {
        array<int, 3> dp = { INF, INF, INF };
        int odd = (h[x].size() % 2);
        dp[0] = 0;
        dp[odd] = 0;
        for (int y : g[x]) if (y != parent) {
            auto [dp1, odd1] = go(y, x);
            array<int, 3> dp2 = { INF, INF, INF };
            REP (i, 3) REP (j, 3 - i) {
                chmin(dp2[i + j], dp[i] + dp1[j] + (odd1 - j + 2) % 2);
            }
            dp = dp2;
            odd += odd1;
        }
        return make_pair(dp, odd);
    };
    return (n - 1) + go(0, -1).first[2];
}

int main() {
    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<vector<int> > h(n);
    REP (i, n - 1) {
        int c, d; cin >> c >> d;
        -- c;
        -- d;
        h[c].push_back(c);
        h[d].push_back(d);
    }
    cout << solve(n, g, h) << endl;
    return 0;
}
0