結果

問題 No.912 赤黒木
ユーザー n_vipn_vip
提出日時 2019-10-18 22:36:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,278 ms / 3,000 ms
コード長 2,628 bytes
コンパイル時間 1,426 ms
コンパイル使用メモリ 135,452 KB
実行使用メモリ 44,632 KB
最終ジャッジ日時 2023-09-08 00:31:47
合計ジャッジ時間 23,473 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1,246 ms
27,812 KB
testcase_13 AC 1,212 ms
28,276 KB
testcase_14 AC 1,146 ms
26,616 KB
testcase_15 AC 1,238 ms
28,268 KB
testcase_16 AC 1,006 ms
27,436 KB
testcase_17 AC 1,132 ms
27,732 KB
testcase_18 AC 1,128 ms
26,720 KB
testcase_19 AC 1,206 ms
26,176 KB
testcase_20 AC 1,278 ms
26,052 KB
testcase_21 AC 1,005 ms
26,016 KB
testcase_22 AC 455 ms
29,660 KB
testcase_23 AC 417 ms
29,680 KB
testcase_24 AC 464 ms
29,628 KB
testcase_25 AC 1,025 ms
27,564 KB
testcase_26 AC 1,091 ms
28,540 KB
testcase_27 AC 1,267 ms
27,080 KB
testcase_28 AC 842 ms
44,632 KB
testcase_29 AC 798 ms
44,236 KB
testcase_30 AC 807 ms
44,556 KB
testcase_31 AC 800 ms
42,044 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()>30) vs.resize(30);
	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