結果

問題 No.912 赤黒木
ユーザー n_vipn_vip
提出日時 2019-10-18 22:35:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,393 ms / 3,000 ms
コード長 2,628 bytes
コンパイル時間 1,445 ms
コンパイル使用メモリ 135,960 KB
実行使用メモリ 44,668 KB
最終ジャッジ日時 2023-09-08 00:29:51
合計ジャッジ時間 26,108 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 1,165 ms
27,880 KB
testcase_13 AC 1,277 ms
28,264 KB
testcase_14 AC 1,270 ms
26,704 KB
testcase_15 AC 1,393 ms
28,508 KB
testcase_16 AC 1,268 ms
27,360 KB
testcase_17 AC 1,320 ms
27,732 KB
testcase_18 AC 1,268 ms
26,740 KB
testcase_19 AC 1,249 ms
26,204 KB
testcase_20 AC 1,232 ms
26,160 KB
testcase_21 AC 1,246 ms
25,908 KB
testcase_22 AC 522 ms
29,676 KB
testcase_23 AC 515 ms
29,684 KB
testcase_24 AC 537 ms
29,648 KB
testcase_25 AC 1,135 ms
27,560 KB
testcase_26 AC 1,223 ms
28,340 KB
testcase_27 AC 1,111 ms
27,148 KB
testcase_28 AC 1,296 ms
44,668 KB
testcase_29 AC 1,234 ms
44,360 KB
testcase_30 AC 1,186 ms
44,624 KB
testcase_31 AC 1,098 ms
41,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#include<cassert>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define Endl endl
#define NL <<"\n"

using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<class T> using vv=vector<vector<T>>;
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
//#undef NUIP
#ifdef NUIP
#include "../benri.h"
#else
#define out(args...)
#endif
#ifdef __cpp_init_captures
template<typename T>vector<T> table(int n, T v){ return vector<T>(n, v);}
template <class... Args> auto table(int n, Args... args){auto val = table(args...); return vector<decltype(val)>(n, move(val));}
#endif
const ll MOD=1e9+7; //998244353

int main(){
  ios_base::sync_with_stdio(false); cin.tie(0);
  cout<<fixed<<setprecision(0);
	int n;
	cin>>n;
	vv<int> g(n);
	rep(i,n-1){
		int a,b;
		cin>>a>>b; --a; --b;
		g[a].pb(b);
		g[b].pb(a);
	}
	vector<int> cnt(n);
	rep(i,n-1){
		int a,b;
		cin>>a>>b; --a; --b;
		++cnt[a]; ++cnt[b];
	}
	out(cnt,1);
	int res=0;
	vv<pii> h(n);
	{
	function<int(int,int)> dfs=
		[&](int v,int p){
			int re=cnt[v]%2;
			for(int w:g[v])if(w!=p) re+=dfs(w,v);
			if(p>=0){
				h[v].eb(p,re%2);
				h[p].eb(v,re%2);
			}
			if(re%2){
				++res;
				out(v,p,re,1);
			}
			return re;
		};
	dfs(0,-1);
	}
	int len=0;
	vector<int> vs(n); iota(all(vs),0); random_shuffle(all(vs));
	if(vs.size()>50) vs.resize(50);
	for(int rt:vs){
		vector<int> usd(n);
	function<pii(int,int)> dfs=
		[&](int v,int p){
			MX(usd[v],1);
			pii re(0,v);
			for(pii w:h[v])if(w.X!=p){
					pii p=dfs(w.X,v);
					if(w.Y) ++p.X;
					else --p.X;
					MX(re,p);
				}
			return re;
		};
	MX(len,dfs(dfs(rt,-1).Y,-1).X);
	}
	out(len,1);
	cout<<res-len+n-1 NL;
  return 0;
}
0