0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 15:16:28 +00:00

1864. [Zjoi2006]三色二叉树

https://hydro.ac/d/bzoj/record/6322eda9bdf9bc31d160a1ca
This commit is contained in:
Baoshuo Ren 2022-09-15 17:18:18 +08:00
parent 1d2f77ea7d
commit 487d68a098
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
27 changed files with 172 additions and 0 deletions

94
BZOJ/1864/1864.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;
}

BIN
BZOJ/1864/data/1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/11.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/11.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/12.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/12.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/13.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/13.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/1864/data/9.out (Stored with Git LFS) Normal file

Binary file not shown.