結果

問題 No.912 赤黒木
ユーザー milanis48663220milanis48663220
提出日時 2023-07-25 23:55:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 486 ms / 3,000 ms
コード長 2,533 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 130,944 KB
実行使用メモリ 92,940 KB
最終ジャッジ日時 2024-04-10 15:07:58
合計ジャッジ時間 12,117 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 350 ms
53,120 KB
testcase_13 AC 372 ms
54,656 KB
testcase_14 AC 359 ms
51,480 KB
testcase_15 AC 363 ms
54,912 KB
testcase_16 AC 352 ms
52,808 KB
testcase_17 AC 365 ms
53,504 KB
testcase_18 AC 324 ms
51,632 KB
testcase_19 AC 351 ms
50,760 KB
testcase_20 AC 378 ms
50,304 KB
testcase_21 AC 365 ms
50,048 KB
testcase_22 AC 316 ms
56,128 KB
testcase_23 AC 327 ms
56,228 KB
testcase_24 AC 334 ms
56,220 KB
testcase_25 AC 389 ms
53,232 KB
testcase_26 AC 397 ms
54,828 KB
testcase_27 AC 378 ms
52,608 KB
testcase_28 AC 486 ms
92,288 KB
testcase_29 AC 480 ms
91,648 KB
testcase_30 AC 460 ms
92,940 KB
testcase_31 AC 414 ms
87,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

const ll inf = 1e18;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<vector<int>> g_red(n), g_black(n);
    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--; b--;
        g_red[a].push_back(b);
        g_red[b].push_back(a);
    }
    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--; b--;
        g_black[a].push_back(b);
        g_black[b].push_back(a);
    }
    auto dp = vec3d(n, 2, 3, inf);
    function<void(int, int)> dfs = [&](int v, int par){
        if(g_black[v].size()%2 == 1){
            dp[v][1][0] = 0;
            dp[v][0][1] = 0;
        }else{
            dp[v][0][0] = 0;
        }
        for(int to: g_red[v]){
            if(to == par) continue;
            dfs(to, v);
            auto nx = vec2d(2, 3, inf);
            for(int i = 0; i < 2; i++){
                for(int j = 0; j < 3; j++){
                    for(int ii = 0; ii < 2; ii++){
                        for(int jj = 0; jj < 3; jj++){
                            if(j+jj > 2) continue;
                            chmin(nx[i^ii][j+jj], dp[v][i][j]+dp[to][ii][jj]+ii);
                        }
                    }
                }
            }
            dp[v] = nx;
        }
    };
    dfs(0, -1);
    cout << dp[0][0][2]+(n-1) << endl;
}
0