結果

問題 No.912 赤黒木
ユーザー yosupotyosupot
提出日時 2019-10-18 22:49:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 311 ms / 3,000 ms
コード長 2,786 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 207,992 KB
実行使用メモリ 43,836 KB
最終ジャッジ日時 2023-09-08 00:57:05
合計ジャッジ時間 9,695 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 263 ms
16,688 KB
testcase_13 AC 256 ms
17,116 KB
testcase_14 AC 226 ms
16,268 KB
testcase_15 AC 238 ms
17,136 KB
testcase_16 AC 235 ms
16,648 KB
testcase_17 AC 248 ms
16,728 KB
testcase_18 AC 243 ms
16,168 KB
testcase_19 AC 217 ms
15,812 KB
testcase_20 AC 224 ms
15,904 KB
testcase_21 AC 236 ms
15,868 KB
testcase_22 AC 183 ms
19,360 KB
testcase_23 AC 182 ms
19,344 KB
testcase_24 AC 176 ms
19,368 KB
testcase_25 AC 237 ms
16,720 KB
testcase_26 AC 250 ms
17,144 KB
testcase_27 AC 234 ms
16,432 KB
testcase_28 AC 285 ms
43,836 KB
testcase_29 AC 296 ms
43,584 KB
testcase_30 AC 311 ms
43,704 KB
testcase_31 AC 283 ms
41,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
//#undef LOCAL
#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#ifdef LOCAL
struct PrettyOS {
    ostream& os;
    bool first;
    template <class T> auto operator<<(T&& x) {
        if (!first) os << ", ";
        first = false;
        os << x;
        return *this;
    }
};
template <class... T> void dbg0(T&&... t) {
    (PrettyOS{cerr, true} << ... << t);
}
#define dbg(...)                                            \
    do {                                                    \
        cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
        dbg0(__VA_ARGS__);                                  \
        cerr << endl;                                       \
    } while (false);
#else
#define dbg(...)
#endif

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "P(" << p.first << ", " << p.second << ")";
}

template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
    os << "[";
    for (auto d : v) os << d << ", ";
    return os << "]";
}

VV<int> g;
V<int> dif, odd, sz;

void dfs(int p, int b) {
    for (int d: g[p]) {
        if (d == b) continue;
        dfs(d, p);
        sz[p] ^= sz[d];
    }
}


int ma = 0;
int dfs2(int p, int b) {
    V<int> top;
    top.push_back(-TEN(8));
    top.push_back(-TEN(8));
    if (odd[p]) top.push_back(dif[p]);
    for (int d: g[p]) {
        if (d == b) continue;
        dif[d] = dif[p];
        if (sz[d]) dif[d]++;
        else dif[d]--;

        top.push_back(dfs2(d, p));
    }
    sort(top.begin(), top.end(), greater<ll>());
    ma = max(ma, top[0] + top[1] - 2 * dif[p]);
    return top[0];
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int n;
    cin >> n;

    g = VV<int>(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);
    }

    V<int> deg(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b; a--; b--;
        deg[a] = 1 - deg[a];
        deg[b] = 1 - deg[b];
    }
    odd = V<int>(n);
    sz = V<int>(n);
    for (int i = 0; i < n; i++) {
        if (deg[i] % 2) {
            odd[i] = sz[i] = 1;
        }
    }
    dfs(0, -1);
    dbg(odd, sz);
    assert(sz[0] == 0);

    int ans = n - 1;
    for (int i = 0; i < n; i++) {
        if (sz[i]) ans++;
    }
    dif = V<int>(n);
    dfs2(0, -1);
    dbg(ans, ma);
    cout << ans - ma << endl;

    return 0;
}
0