結果

問題 No.912 赤黒木
ユーザー n_vipn_vip
提出日時 2019-10-18 22:36:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,914 ms / 3,000 ms
コード長 2,628 bytes
コンパイル時間 1,567 ms
コンパイル使用メモリ 132,328 KB
最終ジャッジ日時 2025-01-07 23:18:07
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:98:59: warning: ‘void std::random_shuffle(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<int*, vector<int, allocator<int> > >]’ is deprecated: use 'std::shuffle' instead [-Wdeprecated-declarations]
   98 |         vector<int> vs(n); iota(all(vs),0); random_shuffle(all(vs));
      |                                             ~~~~~~~~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
                 from main.cpp:9:
/usr/include/c++/13/bits/stl_algo.h:4581:5: note: declared here
 4581 |     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~~~~~~~~~~~

ソースコード

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