結果

問題 No.912 赤黒木
ユーザー square1001square1001
提出日時 2019-10-18 22:37:22
言語 C++14
(gcc 12.3.0 + boost 1.83.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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 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 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 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 371 ms
15,488 KB
testcase_13 AC 385 ms
15,872 KB
testcase_14 AC 358 ms
14,976 KB
testcase_15 AC 397 ms
15,872 KB
testcase_16 AC 371 ms
15,360 KB
testcase_17 AC 378 ms
15,488 KB
testcase_18 AC 361 ms
14,976 KB
testcase_19 AC 348 ms
14,720 KB
testcase_20 AC 349 ms
14,848 KB
testcase_21 AC 345 ms
14,592 KB
testcase_22 AC 333 ms
16,448 KB
testcase_23 AC 333 ms
16,668 KB
testcase_24 AC 333 ms
16,536 KB
testcase_25 AC 370 ms
15,360 KB
testcase_26 AC 384 ms
15,744 KB
testcase_27 AC 362 ms
15,232 KB
testcase_28 AC 426 ms
54,528 KB
testcase_29 AC 420 ms
54,144 KB
testcase_30 AC 423 ms
54,400 KB
testcase_31 AC 397 ms
51,328 KB
権限があれば一括ダウンロードができます

ソースコード

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;
}
0