結果

問題 No.912 赤黒木
ユーザー kwm_tkwm_t
提出日時 2023-05-13 16:17:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 3,000 ms
コード長 1,743 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 203,608 KB
実行使用メモリ 40,492 KB
最終ジャッジ日時 2023-08-19 15:03:47
合計ジャッジ時間 11,563 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,556 KB
testcase_01 AC 4 ms
7,700 KB
testcase_02 AC 5 ms
7,496 KB
testcase_03 AC 4 ms
7,484 KB
testcase_04 AC 4 ms
7,600 KB
testcase_05 AC 5 ms
7,596 KB
testcase_06 AC 4 ms
7,508 KB
testcase_07 AC 5 ms
7,572 KB
testcase_08 AC 4 ms
7,604 KB
testcase_09 AC 4 ms
7,468 KB
testcase_10 AC 4 ms
7,668 KB
testcase_11 AC 5 ms
7,684 KB
testcase_12 AC 211 ms
16,468 KB
testcase_13 AC 225 ms
16,860 KB
testcase_14 AC 207 ms
16,080 KB
testcase_15 AC 227 ms
16,716 KB
testcase_16 AC 214 ms
16,452 KB
testcase_17 AC 215 ms
16,876 KB
testcase_18 AC 211 ms
16,120 KB
testcase_19 AC 207 ms
16,128 KB
testcase_20 AC 206 ms
15,864 KB
testcase_21 AC 201 ms
15,912 KB
testcase_22 AC 147 ms
17,528 KB
testcase_23 AC 153 ms
17,540 KB
testcase_24 AC 156 ms
17,708 KB
testcase_25 AC 215 ms
16,464 KB
testcase_26 AC 205 ms
16,716 KB
testcase_27 AC 198 ms
16,372 KB
testcase_28 AC 245 ms
40,492 KB
testcase_29 AC 247 ms
40,480 KB
testcase_30 AC 247 ms
40,468 KB
testcase_31 AC 241 ms
38,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
vector<vector<int>>to(200005);
bool deg[200005], sum[200005];
int dp[200000][3];
void predfs(int idx, int p = -1) {
	sum[idx] = deg[idx];
	for (auto &nv : to[idx]) {
		if (p == nv)continue;
		predfs(nv, idx);
		sum[idx] ^= sum[nv];
	}
}
void dfs(int v, int p = -1) {
	vector<int>sub(3, 0);
	for (auto nv : to[v]) {
		if (p == nv)continue;
		dfs(nv, v);
		vector<int>subsub(3, INF);
		rep(i, 3)rep(j, 3) {
			if (i + j >= 3)continue;
			chmin(subsub[i+j],sub[i] + dp[nv][j] + (j + sum[nv]) % 2);
		}
		swap(sub, subsub);
	}
	rep(i, 3) dp[v][i] = sub[i];
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	rep(i, n - 1) {
		int a, b; cin >> a >> b;
		a--, b--;
		to[a].push_back(b);
		to[b].push_back(a);
	}
	rep(i, n - 1) {
		int a, b; cin >> a >> b;
		a--, b--;
		deg[a] ^= 1;
		deg[b] ^= 1;
	}
	predfs(0);
	dfs(0);
	cout << min(dp[0][0], dp[0][2]) + n - 1 << endl;
	return 0;
}
0