結果

問題 No.912 赤黒木
ユーザー n_vipn_vip
提出日時 2019-10-18 22:06:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,506 bytes
コンパイル時間 1,246 ms
コンパイル使用メモリ 130,456 KB
実行使用メモリ 43,792 KB
最終ジャッジ日時 2023-09-07 23:34:24
合計ジャッジ時間 7,914 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 173 ms
28,148 KB
testcase_23 AC 177 ms
27,872 KB
testcase_24 AC 177 ms
28,032 KB
testcase_25 AC 265 ms
25,852 KB
testcase_26 AC 273 ms
26,512 KB
testcase_27 AC 266 ms
25,600 KB
testcase_28 AC 237 ms
39,764 KB
testcase_29 AC 222 ms
38,536 KB
testcase_30 AC 274 ms
43,792 KB
testcase_31 AC 253 ms
41,196 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<int> 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(re%2){
				++res;
				out(v,p,re,1);
				h[v].pb(p);
				h[p].pb(v);
			}
			return re;
		};
	dfs(0,-1);
	}
	int len=0;
	{
		vector<int> usd(n);
	function<pii(int,int)> dfs=
		[&](int v,int p){
			MX(usd[v],1);
			pii re(-1,v);
			for(int w:h[v])if(w!=p) MX(re,dfs(w,v));
			++re.X;
			return re;
		};
	rep(v,n)if(MX(usd[v],1)){
		pii w=dfs(v,-1);
		pii l=dfs(w.Y,-1);
		out(v,w,l,1);
		MX(len,l.X);
	}
	}
	out(h,len,1);
	cout<<res-len+n-1 NL;
  return 0;
}
0