結果

問題 No.912 赤黒木
ユーザー square1001square1001
提出日時 2019-10-18 22:37:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 369 ms / 3,000 ms
コード長 1,069 bytes
コンパイル時間 824 ms
コンパイル使用メモリ 81,948 KB
実行使用メモリ 54,276 KB
最終ジャッジ日時 2023-09-08 00:35:36
合計ジャッジ時間 9,624 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 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 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 319 ms
15,060 KB
testcase_13 AC 332 ms
15,564 KB
testcase_14 AC 311 ms
14,772 KB
testcase_15 AC 339 ms
15,796 KB
testcase_16 AC 319 ms
15,164 KB
testcase_17 AC 305 ms
15,448 KB
testcase_18 AC 303 ms
14,928 KB
testcase_19 AC 306 ms
14,816 KB
testcase_20 AC 297 ms
14,576 KB
testcase_21 AC 299 ms
14,316 KB
testcase_22 AC 297 ms
16,356 KB
testcase_23 AC 287 ms
16,320 KB
testcase_24 AC 290 ms
16,676 KB
testcase_25 AC 328 ms
15,168 KB
testcase_26 AC 331 ms
15,592 KB
testcase_27 AC 309 ms
15,116 KB
testcase_28 AC 367 ms
54,192 KB
testcase_29 AC 354 ms
54,136 KB
testcase_30 AC 369 ms
54,276 KB
testcase_31 AC 342 ms
51,556 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