結果

問題 No.912 赤黒木
ユーザー square1001
提出日時 2019-10-18 22:37:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 426 ms / 3,000 ms
コード長 1,069 bytes
コンパイル時間 808 ms
コンパイル使用メモリ 81,940 KB
実行使用メモリ 54,528 KB
最終ジャッジ日時 2024-06-25 17:46:19
合計ジャッジ時間 10,129 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
const int inf = 1012345678;
int main() {
int N;
cin >> N;
vector<vector<int> > G(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);
}
vector<int> d(N);
for (int i = 0; i < N - 1; ++i) {
int a, b;
cin >> a >> b; --a, --b;
d[a] ^= 1;
d[b] ^= 1;
}
vector<int> cnt(N);
function<vector<int>(int, int)> dfs = [&](int pos, int pre) {
cnt[pos] = d[pos];
vector<int> ans = { 0, inf, inf };
if (d[pos] == 1) ans[1] = 0;
for (int i : G[pos]) {
if (i == pre) continue;
vector<int> res = dfs(i, pos);
for (int j = 0; j <= 2; ++j) {
if ((cnt[i] + j) % 2 == 1) ++res[j];
}
cnt[pos] ^= cnt[i];
vector<int> ndp(3, inf);
for (int j = 0; j <= 2; ++j) {
for (int k = 0; j + k <= 2; ++k) {
ndp[j + k] = min(ndp[j + k], ans[j] + res[k]);
}
}
ans = ndp;
}
return ans;
};
vector<int> ret = dfs(0, -1);
cout << N - 1 + ret[2] << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0