結果

問題 No.912 赤黒木
ユーザー sigma425sigma425
提出日時 2019-10-18 23:27:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 373 ms / 3,000 ms
コード長 2,082 bytes
コンパイル時間 2,277 ms
コンパイル使用メモリ 210,140 KB
実行使用メモリ 96,344 KB
最終ジャッジ日時 2023-09-08 02:43:17
合計ジャッジ時間 10,869 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 275 ms
15,088 KB
testcase_13 AC 285 ms
15,664 KB
testcase_14 AC 245 ms
14,820 KB
testcase_15 AC 275 ms
15,696 KB
testcase_16 AC 274 ms
15,088 KB
testcase_17 AC 271 ms
15,408 KB
testcase_18 AC 257 ms
14,760 KB
testcase_19 AC 256 ms
14,648 KB
testcase_20 AC 259 ms
14,500 KB
testcase_21 AC 250 ms
14,560 KB
testcase_22 AC 243 ms
16,316 KB
testcase_23 AC 247 ms
16,372 KB
testcase_24 AC 246 ms
16,512 KB
testcase_25 AC 280 ms
15,148 KB
testcase_26 AC 287 ms
15,604 KB
testcase_27 AC 279 ms
15,144 KB
testcase_28 AC 338 ms
96,324 KB
testcase_29 AC 337 ms
95,968 KB
testcase_30 AC 373 ms
96,344 KB
testcase_31 AC 344 ms
90,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);
	
	int N;
	cin >> N;
	V<int> d(N);
	VV<int> G(N);
	rep(i,N-1){
		int x,y;
		cin >> x >> y; x--, y--;
		G[x].pb(y);G[y].pb(x);
	}
	rep(i,N-1){
		int x,y;
		cin >> x >> y; x--, y--;
		d[x]++,d[y]++;
	}
	V<int> depth(N);
	int inf = 1e9;
	function<VV<int>(int,int)> dfs = [&](int v,int p){	//erase_num,odd
		depth[v] = p == -1 ? 0 : depth[p] + 1;

		VV<int> cur(3,V<int>(2,inf)); cur[0][0] = 0;
		for(int u: G[v]) if(u != p){
			auto d = dfs(u,v);
			VV<int> nxt(3,V<int>(2,inf));
			rep(i,3) rep(j,2) if(cur[i][j] != inf){
				rep(k,3) rep(l,2) if(d[k][l] != inf){
					if(i+k > 2) continue;
					int add = (j+l==2) ? -2*depth[v] : 0;
					chmin(nxt[i+k][(j+l)%2],cur[i][j]+d[k][l]+add);
				}
			}
			cur = nxt;
		}
		if(d[v]%2){
			VV<int> nxt(3,V<int>(2,inf));
			rep(i,3) rep(j,2) if(cur[i][j] != inf){
				if(i<2){	//erase v
					chmin(nxt[i+1][j],cur[i][j]);
				}
				if(j == 0){
					chmin(nxt[i][1],cur[i][j]+depth[v]);
				}else{
					chmin(nxt[i][0],cur[i][j]-depth[v]);
				}
			}
			cur = nxt;
		}
		return cur;
	};
	rep(r,N){
		show(dfs(r,-1));
	}
	auto f = dfs(0,-1);
	cout << N-1 + f[2][0] << endl;
}
0