結果

問題 No.912 赤黒木
ユーザー milanis48663220
提出日時 2023-07-25 23:55:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 412 ms / 3,000 ms
コード長 2,533 bytes
コンパイル時間 1,376 ms
コンパイル使用メモリ 127,692 KB
最終ジャッジ日時 2025-02-15 19:08:23
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
return vector<vector<T>>(n, vector<T>(m, v));
}
template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}
template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
if(v.empty()) {
cout << endl;
return;
}
for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
cout << v.back() << endl;
}
const ll inf = 1e18;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n; cin >> n;
vector<vector<int>> g_red(n), g_black(n);
for(int i = 0; i < n-1; i++){
int a, b; cin >> a >> b; a--; b--;
g_red[a].push_back(b);
g_red[b].push_back(a);
}
for(int i = 0; i < n-1; i++){
int a, b; cin >> a >> b; a--; b--;
g_black[a].push_back(b);
g_black[b].push_back(a);
}
auto dp = vec3d(n, 2, 3, inf);
function<void(int, int)> dfs = [&](int v, int par){
if(g_black[v].size()%2 == 1){
dp[v][1][0] = 0;
dp[v][0][1] = 0;
}else{
dp[v][0][0] = 0;
}
for(int to: g_red[v]){
if(to == par) continue;
dfs(to, v);
auto nx = vec2d(2, 3, inf);
for(int i = 0; i < 2; i++){
for(int j = 0; j < 3; j++){
for(int ii = 0; ii < 2; ii++){
for(int jj = 0; jj < 3; jj++){
if(j+jj > 2) continue;
chmin(nx[i^ii][j+jj], dp[v][i][j]+dp[to][ii][jj]+ii);
}
}
}
}
dp[v] = nx;
}
};
dfs(0, -1);
cout << dp[0][0][2]+(n-1) << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0