diff --git a/BZOJ/1917/1917.cpp b/BZOJ/1917/1917.cpp new file mode 100644 index 00000000..94c15baf --- /dev/null +++ b/BZOJ/1917/1917.cpp @@ -0,0 +1,84 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 50005; + +int n, h[N], son[N], ans[N], res; +std::vector g[N]; + +void dfs1(int u, int f) { + for (int v : g[u]) { + if (v == f) continue; + + dfs1(v, u); + + int x = std::min(h[u], h[v]); + + h[u] -= x, h[v] -= x; + res += x * 2; + + if (h[v]) son[u] = v; + } +} + +void dfs2(int u, int f) { + ans[u] = res; + + for (int v : g[u]) { + if (v == f) continue; + + if (h[u]) { + h[u]--, res++; + + dfs2(v, u); + + h[u]++, res--; + } else if (son[v]) { + h[son[v]]--, res++; + + dfs2(v, u); + + h[son[v]]++, res--; + } else { + h[v]++, res--; + + dfs2(v, u); + + h[v]--, res++; + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> h[i]; + } + + for (int i = 1, x, y; i < n; i++) { + cin >> x >> y; + + h[++x]--, h[++y]--; + g[x].emplace_back(y); + g[y].emplace_back(x); + + res += 2; + } + + dfs1(1, 0); + dfs2(1, 0); + + for (int i = 1; i <= n; i++) { + cout << ans[i] << endl; + } + + return 0; +} diff --git a/BZOJ/1917/data/1.in b/BZOJ/1917/data/1.in new file mode 100644 index 00000000..35e540fc --- /dev/null +++ b/BZOJ/1917/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3d749211baeba65e96ed7e60dcc7c5b381be1c31f6605424a2337289c6c61d5a +size 836797 diff --git a/BZOJ/1917/data/1.out b/BZOJ/1917/data/1.out new file mode 100644 index 00000000..ec100961 --- /dev/null +++ b/BZOJ/1917/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:87ba92235d6a30fbc8e9c8837b8c674ef2856e1b8d59f3f378b5a642dd7a6584 +size 500000 diff --git a/BZOJ/1917/data/10.in b/BZOJ/1917/data/10.in new file mode 100644 index 00000000..e81f7500 --- /dev/null +++ b/BZOJ/1917/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1fcbb566a677822b725e2669492f66f557e53949bff12347128e41e030ca40f8 +size 6412 diff --git a/BZOJ/1917/data/10.out b/BZOJ/1917/data/10.out new file mode 100644 index 00000000..8a13eece --- /dev/null +++ b/BZOJ/1917/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a32d4f08a9631cbb9dd1e9ef89be67596cbba358f5112fe3167997f458da1df1 +size 4000 diff --git a/BZOJ/1917/data/2.in b/BZOJ/1917/data/2.in new file mode 100644 index 00000000..e622c661 --- /dev/null +++ b/BZOJ/1917/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d45e292bd502c1b32bb49f5061aa84eea0679a6d8a315ead4688d638992002db +size 836673 diff --git a/BZOJ/1917/data/2.out b/BZOJ/1917/data/2.out new file mode 100644 index 00000000..277cd976 --- /dev/null +++ b/BZOJ/1917/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5858e22c504294c882c2dce817add67d5e0405f913e50161b88d784d65adae31 +size 500000 diff --git a/BZOJ/1917/data/3.in b/BZOJ/1917/data/3.in new file mode 100644 index 00000000..1b099601 --- /dev/null +++ b/BZOJ/1917/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1d6ad9cb325db48c09c26a8eadd451cc4e1579936e92146026216a9891888a97 +size 837073 diff --git a/BZOJ/1917/data/3.out b/BZOJ/1917/data/3.out new file mode 100644 index 00000000..be028b20 --- /dev/null +++ b/BZOJ/1917/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cb761bbcfb7e8996e2838554b78c2c5a2ac956da9646de222256dff601150a90 +size 500000 diff --git a/BZOJ/1917/data/4.in b/BZOJ/1917/data/4.in new file mode 100644 index 00000000..d531f375 --- /dev/null +++ b/BZOJ/1917/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d78841f5b7597c2ff12fa413b2993f0d550efdc2a558d49fb3693adc72d22d06 +size 6397 diff --git a/BZOJ/1917/data/4.out b/BZOJ/1917/data/4.out new file mode 100644 index 00000000..f4db745c --- /dev/null +++ b/BZOJ/1917/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f1e3cafd377c93ad72a3cda1a7d5c27cc3c62605de36ebff795fde4d497ece28 +size 4000 diff --git a/BZOJ/1917/data/5.in b/BZOJ/1917/data/5.in new file mode 100644 index 00000000..6d44b38f --- /dev/null +++ b/BZOJ/1917/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ce4804cb50631829d3f5a59e6b4b2d71bed28e5a63988cb9ea42a73e85f1d6a9 +size 6423 diff --git a/BZOJ/1917/data/5.out b/BZOJ/1917/data/5.out new file mode 100644 index 00000000..11239762 --- /dev/null +++ b/BZOJ/1917/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ae0ce30772a169d6da45de13713a65760ff986f359e5c24533cb2a3848e7eed5 +size 4000 diff --git a/BZOJ/1917/data/6.in b/BZOJ/1917/data/6.in new file mode 100644 index 00000000..d1dc0769 --- /dev/null +++ b/BZOJ/1917/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7f0f36068c468050f0e11b312d2b5b5d4e1f7748a7c7a7e59ee00ca1562cf7c1 +size 6418 diff --git a/BZOJ/1917/data/6.out b/BZOJ/1917/data/6.out new file mode 100644 index 00000000..5dbf1c69 --- /dev/null +++ b/BZOJ/1917/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0835e9a605d78a2aee3e3087b8aa6c74db42de7c71274e235408a85f80e1e4ea +size 4000 diff --git a/BZOJ/1917/data/7.in b/BZOJ/1917/data/7.in new file mode 100644 index 00000000..8a204e11 --- /dev/null +++ b/BZOJ/1917/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:682461cb94577a597463e4a13feb1d5c74d23fbc9fb0d9ced2deee831f03a2cd +size 836798 diff --git a/BZOJ/1917/data/7.out b/BZOJ/1917/data/7.out new file mode 100644 index 00000000..468b6863 --- /dev/null +++ b/BZOJ/1917/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a65bfbdfba7003969ad8d37285525a3359286069b50db423d61aa99dc46be32b +size 500000 diff --git a/BZOJ/1917/data/8.in b/BZOJ/1917/data/8.in new file mode 100644 index 00000000..1bb9e34f --- /dev/null +++ b/BZOJ/1917/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8154285443884265c431e47449c645f9752b0293c93c14e48e050d55f9b795af +size 836820 diff --git a/BZOJ/1917/data/8.out b/BZOJ/1917/data/8.out new file mode 100644 index 00000000..e5194e4c --- /dev/null +++ b/BZOJ/1917/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1c0ef96d2cf6ad9fa559ef4927d70ee56c70be0f59e7bc5296b2f35eb250a6ca +size 500000 diff --git a/BZOJ/1917/data/9.in b/BZOJ/1917/data/9.in new file mode 100644 index 00000000..adf9f86f --- /dev/null +++ b/BZOJ/1917/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:586babefdad5429e5bc61f8d3c26f72f3ec91fe02a503ada84f4b12c694168ad +size 836660 diff --git a/BZOJ/1917/data/9.out b/BZOJ/1917/data/9.out new file mode 100644 index 00000000..099fa4bd --- /dev/null +++ b/BZOJ/1917/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a0efa4b2c5e397b8933ce8eb9cc3e413e58be138500e9d83dd41bab6ba53773a +size 500000