From 487d68a098e0452aa0726600025dcab541680f44 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 15 Sep 2022 17:18:18 +0800 Subject: [PATCH] =?UTF-8?q?1864.=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://hydro.ac/d/bzoj/record/6322eda9bdf9bc31d160a1ca --- BZOJ/1864/1864.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++ BZOJ/1864/data/1.in | 3 ++ BZOJ/1864/data/1.out | 3 ++ BZOJ/1864/data/10.in | 3 ++ BZOJ/1864/data/10.out | 3 ++ BZOJ/1864/data/11.in | 3 ++ BZOJ/1864/data/11.out | 3 ++ BZOJ/1864/data/12.in | 3 ++ BZOJ/1864/data/12.out | 3 ++ BZOJ/1864/data/13.in | 3 ++ BZOJ/1864/data/13.out | 3 ++ BZOJ/1864/data/2.in | 3 ++ BZOJ/1864/data/2.out | 3 ++ BZOJ/1864/data/3.in | 3 ++ BZOJ/1864/data/3.out | 3 ++ BZOJ/1864/data/4.in | 3 ++ BZOJ/1864/data/4.out | 3 ++ BZOJ/1864/data/5.in | 3 ++ BZOJ/1864/data/5.out | 3 ++ BZOJ/1864/data/6.in | 3 ++ BZOJ/1864/data/6.out | 3 ++ BZOJ/1864/data/7.in | 3 ++ BZOJ/1864/data/7.out | 3 ++ BZOJ/1864/data/8.in | 3 ++ BZOJ/1864/data/8.out | 3 ++ BZOJ/1864/data/9.in | 3 ++ BZOJ/1864/data/9.out | 3 ++ 27 files changed, 172 insertions(+) create mode 100644 BZOJ/1864/1864.cpp create mode 100644 BZOJ/1864/data/1.in create mode 100644 BZOJ/1864/data/1.out create mode 100644 BZOJ/1864/data/10.in create mode 100644 BZOJ/1864/data/10.out create mode 100644 BZOJ/1864/data/11.in create mode 100644 BZOJ/1864/data/11.out create mode 100644 BZOJ/1864/data/12.in create mode 100644 BZOJ/1864/data/12.out create mode 100644 BZOJ/1864/data/13.in create mode 100644 BZOJ/1864/data/13.out create mode 100644 BZOJ/1864/data/2.in create mode 100644 BZOJ/1864/data/2.out create mode 100644 BZOJ/1864/data/3.in create mode 100644 BZOJ/1864/data/3.out create mode 100644 BZOJ/1864/data/4.in create mode 100644 BZOJ/1864/data/4.out create mode 100644 BZOJ/1864/data/5.in create mode 100644 BZOJ/1864/data/5.out create mode 100644 BZOJ/1864/data/6.in create mode 100644 BZOJ/1864/data/6.out create mode 100644 BZOJ/1864/data/7.in create mode 100644 BZOJ/1864/data/7.out create mode 100644 BZOJ/1864/data/8.in create mode 100644 BZOJ/1864/data/8.out create mode 100644 BZOJ/1864/data/9.in create mode 100644 BZOJ/1864/data/9.out diff --git a/BZOJ/1864/1864.cpp b/BZOJ/1864/1864.cpp new file mode 100644 index 00000000..b1aba723 --- /dev/null +++ b/BZOJ/1864/1864.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; +} diff --git a/BZOJ/1864/data/1.in b/BZOJ/1864/data/1.in new file mode 100644 index 00000000..c9118e78 --- /dev/null +++ b/BZOJ/1864/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:13d1bf624afebf15ba1a3052edff1c3fef6e969314399176308baf17a81dbc28 +size 501 diff --git a/BZOJ/1864/data/1.out b/BZOJ/1864/data/1.out new file mode 100644 index 00000000..9d5b3bce --- /dev/null +++ b/BZOJ/1864/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6f3ba674875fbd6a5856b6279be69c2ab6be96c6c85bd790ef8697c56d97c35f +size 7 diff --git a/BZOJ/1864/data/10.in b/BZOJ/1864/data/10.in new file mode 100644 index 00000000..804fcc90 --- /dev/null +++ b/BZOJ/1864/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa +size 2 diff --git a/BZOJ/1864/data/10.out b/BZOJ/1864/data/10.out new file mode 100644 index 00000000..b635b29a --- /dev/null +++ b/BZOJ/1864/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f4a8ae8e74ddfb896a256de4e3099911dcaa6a9302591713898069b0bcd6e3d7 +size 4 diff --git a/BZOJ/1864/data/11.in b/BZOJ/1864/data/11.in new file mode 100644 index 00000000..36d5128a --- /dev/null +++ b/BZOJ/1864/data/11.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8273f8c9952ecd02b8baaeec720cd216f17d360746bfe880957225e990ab2b19 +size 10000 diff --git a/BZOJ/1864/data/11.out b/BZOJ/1864/data/11.out new file mode 100644 index 00000000..3fd3f6c1 --- /dev/null +++ b/BZOJ/1864/data/11.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:85087b84787bd3beaffafad7cdb47f6e73ef11887535b587e2cb16dab6db0757 +size 10 diff --git a/BZOJ/1864/data/12.in b/BZOJ/1864/data/12.in new file mode 100644 index 00000000..4203e1f3 --- /dev/null +++ b/BZOJ/1864/data/12.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:252a16d8ecaeb0829062bb92317ec08c5b1616f804481f9a4972c35f3e75c2b2 +size 10001 diff --git a/BZOJ/1864/data/12.out b/BZOJ/1864/data/12.out new file mode 100644 index 00000000..03ecde1d --- /dev/null +++ b/BZOJ/1864/data/12.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:13f305f2a3c8ade65b8b1ffad97bd11d982a20737446d25f346d4c38d86f49a5 +size 7 diff --git a/BZOJ/1864/data/13.in b/BZOJ/1864/data/13.in new file mode 100644 index 00000000..c212191e --- /dev/null +++ b/BZOJ/1864/data/13.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9521fd372e7f52bae4cb2ec801d4ccca02eb2824b7282d8ee8fb1ff79118d64c +size 5001 diff --git a/BZOJ/1864/data/13.out b/BZOJ/1864/data/13.out new file mode 100644 index 00000000..1da2f1c1 --- /dev/null +++ b/BZOJ/1864/data/13.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3d5a638e60444f87a092dd71120f0f3d2ed63a0707263c5f38a68d556aaef32e +size 10 diff --git a/BZOJ/1864/data/2.in b/BZOJ/1864/data/2.in new file mode 100644 index 00000000..3ee9955c --- /dev/null +++ b/BZOJ/1864/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6941cba38ce61451f74602d8b7d6b0226d4e69768af95a7f45402b9e0c7e7727 +size 8 diff --git a/BZOJ/1864/data/2.out b/BZOJ/1864/data/2.out new file mode 100644 index 00000000..d28bc50a --- /dev/null +++ b/BZOJ/1864/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ece3d232c1ca9ef8a80b6fdb1585b8f5cf653b9dd023b0521c8da64db859ffac +size 4 diff --git a/BZOJ/1864/data/3.in b/BZOJ/1864/data/3.in new file mode 100644 index 00000000..719dc521 --- /dev/null +++ b/BZOJ/1864/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fe09e03ae099c49d3c388e7778501ba36ffcd9e99a636bdf626f819317995e36 +size 6 diff --git a/BZOJ/1864/data/3.out b/BZOJ/1864/data/3.out new file mode 100644 index 00000000..a6e45269 --- /dev/null +++ b/BZOJ/1864/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dfac347cb71ae693ba8a54687f7112aff24ec67f88a3c66f69a7bf968fa2ac16 +size 4 diff --git a/BZOJ/1864/data/4.in b/BZOJ/1864/data/4.in new file mode 100644 index 00000000..b21a424c --- /dev/null +++ b/BZOJ/1864/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:96b1f4e0b5b0bcfc5900f686c64fa57092e05d53a13131e75f0c4ea65f7bb459 +size 10001 diff --git a/BZOJ/1864/data/4.out b/BZOJ/1864/data/4.out new file mode 100644 index 00000000..c490541b --- /dev/null +++ b/BZOJ/1864/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f058ce45d7eeabfc666c6e0d8265c31c37f2d2c1fb775a3da62dfd591118343d +size 10 diff --git a/BZOJ/1864/data/5.in b/BZOJ/1864/data/5.in new file mode 100644 index 00000000..6cd247a7 --- /dev/null +++ b/BZOJ/1864/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2a7d72d4bec322af5b090375d1554411b9fe960100eaaaa53d2bc1938c24e388 +size 11 diff --git a/BZOJ/1864/data/5.out b/BZOJ/1864/data/5.out new file mode 100644 index 00000000..380cc009 --- /dev/null +++ b/BZOJ/1864/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b473e10de5a7cd1069016e2a38d54b157b0762f5081ddf80d9cd0eafeba1580f +size 4 diff --git a/BZOJ/1864/data/6.in b/BZOJ/1864/data/6.in new file mode 100644 index 00000000..d68c7628 --- /dev/null +++ b/BZOJ/1864/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2a460171c8fba3c1c3e89ee3872d2c96dd1e5b9831616cd1082db721bd2c0830 +size 51 diff --git a/BZOJ/1864/data/6.out b/BZOJ/1864/data/6.out new file mode 100644 index 00000000..8b9d4b67 --- /dev/null +++ b/BZOJ/1864/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c4d17242023c2bca9f1aea960ef80822992b63fe7ceee76a0372cf5c9e2e2de9 +size 6 diff --git a/BZOJ/1864/data/7.in b/BZOJ/1864/data/7.in new file mode 100644 index 00000000..542da3ce --- /dev/null +++ b/BZOJ/1864/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:59ece3655284b70804c48d9d3f6c7467cedd2db48213c062ef74fbb9245a3a5b +size 10001 diff --git a/BZOJ/1864/data/7.out b/BZOJ/1864/data/7.out new file mode 100644 index 00000000..9df5cb66 --- /dev/null +++ b/BZOJ/1864/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4d5f4f5962e151138044ff4343d9ebdff17b020c5ef15816a7137f4f3a47a538 +size 10 diff --git a/BZOJ/1864/data/8.in b/BZOJ/1864/data/8.in new file mode 100644 index 00000000..28615f82 --- /dev/null +++ b/BZOJ/1864/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c11e3f4837efde2441e23a7b9da02131f53bf59fddeb7147c4ab81afe400460f +size 4 diff --git a/BZOJ/1864/data/8.out b/BZOJ/1864/data/8.out new file mode 100644 index 00000000..46d33a80 --- /dev/null +++ b/BZOJ/1864/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3f11ad6bbc7ecca0b2416b713dee77f1a635c00aaeaa946e14cde1c2bfae56d5 +size 4 diff --git a/BZOJ/1864/data/9.in b/BZOJ/1864/data/9.in new file mode 100644 index 00000000..957e19ca --- /dev/null +++ b/BZOJ/1864/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b2a583152328c5eb7799e4f4aab197f112fef8c6aa1b1174d7bfbc90f7846b7a +size 10001 diff --git a/BZOJ/1864/data/9.out b/BZOJ/1864/data/9.out new file mode 100644 index 00000000..1327f8e7 --- /dev/null +++ b/BZOJ/1864/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:52d1a1cfa743f2e4640c726cde83bc9fa93b914b5cc8922005bdc5874241a95d +size 10