From bc6e2d16dfd1422ffdaef3866065a459dd838e8a Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 10 Jun 2022 19:42:49 +0800 Subject: [PATCH] =?UTF-8?q?P2585=20[ZJOI2006]=E4=B8=89=E8=89=B2=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/77193169 --- Luogu/P2585/P2585.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Luogu/P2585/P2585.cpp diff --git a/Luogu/P2585/P2585.cpp b/Luogu/P2585/P2585.cpp new file mode 100644 index 00000000..b1aba723 --- /dev/null +++ b/Luogu/P2585/P2585.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include + +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 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; +}