結果
問題 | No.912 赤黒木 |
ユーザー |
|
提出日時 | 2019-10-24 15:04:20 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 606 ms / 3,000 ms |
コード長 | 1,341 bytes |
コンパイル時間 | 2,555 ms |
コンパイル使用メモリ | 199,332 KB |
最終ジャッジ日時 | 2025-01-08 00:53:14 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h>#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))using namespace std;template <class T, class U> inline void chmin(T & a, U const & b) { a = min<T>(a, b); }int solve(int n, const vector<vector<int> > & g, const vector<vector<int> > & h) {const int INF = n;function<pair<array<int, 3>, int> (int, int)> go = [&](int x, int parent) {array<int, 3> dp = { INF, INF, INF };int odd = (h[x].size() % 2);dp[0] = 0;dp[odd] = 0;for (int y : g[x]) if (y != parent) {auto [dp1, odd1] = go(y, x);array<int, 3> dp2 = { INF, INF, INF };REP (i, 3) REP (j, 3 - i) {chmin(dp2[i + j], dp[i] + dp1[j] + (odd1 - j + 2) % 2);}dp = dp2;odd += odd1;}return make_pair(dp, odd);};return (n - 1) + go(0, -1).first[2];}int main() {int n; cin >> n;vector<vector<int> > g(n);REP (i, n - 1) {int a, b; cin >> a >> b;-- a;-- b;g[a].push_back(b);g[b].push_back(a);}vector<vector<int> > h(n);REP (i, n - 1) {int c, d; cin >> c >> d;-- c;-- d;h[c].push_back(c);h[d].push_back(d);}cout << solve(n, g, h) << endl;return 0;}