0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 20:28:48 +00:00

P2585 [ZJOI2006]三色二叉树

https://www.luogu.com.cn/record/77193169
This commit is contained in:
Baoshuo Ren 2022-06-10 19:42:49 +08:00
parent 288c72adb0
commit bc6e2d16df
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

94
Luogu/P2585/P2585.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 5e5 + 5;
int n, p = 1, f1[N][3], f2[N][3];
std::string s;
std::vector<int> g[N];
void dfs1(int fa) {
int t = s[p] - '0', u = p++;
if (fa) g[fa].push_back(u);
if (!t) return;
dfs1(u);
if (t == 2) dfs1(u);
}
void dfs2(int u) {
if (g[u].empty()) {
f1[u][0] = f2[u][0] = 1;
f1[u][1] = f1[u][2] = f2[u][1] = f2[u][2] = 0;
return;
}
for (int v : g[u]) {
dfs2(v);
f1[u][0] = std::max(f1[v][1], f1[v][2]) + 1;
f1[u][1] = std::max(f1[v][0], f1[v][2]);
f1[u][2] = std::max({f1[u][2], f1[v][0], f1[v][1]});
f2[u][0] = std::min({f2[u][0], f2[v][1] + 1, f2[v][2] + 1});
f2[u][1] = std::min({f2[u][1], f2[v][0], f2[v][2]});
f2[u][2] = std::min({f2[u][2], f2[v][0], f2[v][1]});
}
switch (g[u].size()) {
case 1: {
int v = g[u][0];
f1[u][0] = std::max(f1[v][1], f1[v][2]) + 1;
f1[u][1] = std::max(f1[v][0], f1[v][2]);
f1[u][2] = std::max(f1[v][0], f1[v][1]);
f2[u][0] = std::min(f2[v][1], f2[v][2]) + 1;
f2[u][1] = std::min(f2[v][0], f2[v][2]);
f2[u][2] = std::min(f2[v][0], f2[v][1]);
break;
}
case 2: {
int v1 = g[u][0],
v2 = g[u][1];
f1[u][0] = std::max(f1[v1][1] + f1[v2][2], f1[v1][2] + f1[v2][1]) + 1;
f1[u][1] = std::max(f1[v1][0] + f1[v2][2], f1[v1][2] + f1[v2][0]);
f1[u][2] = std::max(f1[v1][0] + f1[v2][1], f1[v1][1] + f1[v2][0]);
f2[u][0] = std::min(f2[v1][1] + f2[v2][2], f2[v1][2] + f2[v2][1]) + 1;
f2[u][1] = std::min(f2[v1][0] + f2[v2][2], f2[v1][2] + f2[v2][0]);
f2[u][2] = std::min(f2[v1][0] + f2[v2][1], f2[v1][1] + f2[v2][0]);
break;
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> s;
n = s.size();
s = ' ' + s;
dfs1(0);
memset(f2, 0x3f, sizeof(f2));
dfs2(1);
cout << std::max({f1[1][0], f1[1][1], f1[1][2]}) << ' ' << std::min({f2[1][0], f2[1][1], f2[1][2]}) << endl;
return 0;
}