結果

問題 No.912 赤黒木
ユーザー leaf_1415leaf_1415
提出日時 2019-10-19 01:14:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 339 ms / 3,000 ms
コード長 1,703 bytes
コンパイル時間 1,988 ms
コンパイル使用メモリ 66,016 KB
実行使用メモリ 60,964 KB
最終ジャッジ日時 2023-09-08 03:52:18
合計ジャッジ時間 8,549 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
15,388 KB
testcase_01 AC 6 ms
15,328 KB
testcase_02 AC 7 ms
15,360 KB
testcase_03 AC 7 ms
15,412 KB
testcase_04 AC 6 ms
15,348 KB
testcase_05 AC 7 ms
15,392 KB
testcase_06 AC 7 ms
15,576 KB
testcase_07 AC 7 ms
15,464 KB
testcase_08 AC 6 ms
15,276 KB
testcase_09 AC 7 ms
15,284 KB
testcase_10 AC 6 ms
15,344 KB
testcase_11 AC 6 ms
15,308 KB
testcase_12 AC 301 ms
37,600 KB
testcase_13 AC 310 ms
38,024 KB
testcase_14 AC 284 ms
37,104 KB
testcase_15 AC 307 ms
37,996 KB
testcase_16 AC 281 ms
37,556 KB
testcase_17 AC 274 ms
37,696 KB
testcase_18 AC 271 ms
37,060 KB
testcase_19 AC 263 ms
36,836 KB
testcase_20 AC 268 ms
36,668 KB
testcase_21 AC 261 ms
36,556 KB
testcase_22 AC 210 ms
46,880 KB
testcase_23 AC 218 ms
46,864 KB
testcase_24 AC 214 ms
46,944 KB
testcase_25 AC 282 ms
37,884 KB
testcase_26 AC 295 ms
38,032 KB
testcase_27 AC 291 ms
37,400 KB
testcase_28 AC 339 ms
59,748 KB
testcase_29 AC 336 ms
59,532 KB
testcase_30 AC 310 ms
60,964 KB
testcase_31 AC 294 ms
58,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#define llint long long
#define inf 1e18

using namespace std;

typedef pair<llint, llint> P;

llint n;
vector<llint> G[200005], g[200005];
bool odd[200005];
llint dp[200005][2][3];
llint dp2[200005][2][3];

void dfs(int v, int p, llint d)
{
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == p) continue;
		dfs(G[v][i], v, d+1);
	}
	
	for(int i = 0; i <= G[v].size(); i++){
		for(int j = 0; j < 2; j++){
			for(int k = 0; k <= 2; k++){
				dp2[i][j][k] = inf;
			}
		}
	}
	
	if(odd[v]) dp2[0][0][1] = 0, dp2[0][1][0] = d;
	else dp2[0][0][0] = 0;
	
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == p){
			for(int j = 0; j < 2; j++){
				for(int k = 0; k <= 2; k++){
					dp2[i+1][j][k] = dp2[i][j][k];
				}
			}
			continue;
		}
		int u = G[v][i];
		for(int j = 0; j < 2; j++){
			for(int k = 0; k <= 2; k++){
				
				for(int l = 0; l < 2; l++){
					for(int m = 0; m <= 2; m++){
						if(k+m > 2) continue;
						llint cost = 0;
						if(j+l >= 2) cost = 2*d;
						dp2[i+1][(j+l)%2][k+m] = min(dp2[i+1][(j+l)%2][k+m], dp2[i][j][k] + dp[u][l][m] - cost);
					}
				}
			}
		}
	}
	
	for(int j = 0; j < 2; j++){
		for(int k = 0; k <= 2; k++){
			dp[v][j][k] = dp2[(int)G[v].size()][j][k];
		}
	}
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	llint u, v;
	for(int i = 0; i < n-1; i++){
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	for(int i = 0; i < n-1; i++){
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	
	for(int i = 1; i <= n; i++){
		if((int)g[i].size() % 2) odd[i] = true;
	}
	
	dfs(1, -1, 0);
	
	cout << dp[1][0][2] + n-1 << endl;
	
	return 0;
}
0