From 244f91eef3c0cd33bc092a1ccf444c06826a8f3b Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 15 Sep 2022 17:08:48 +0800 Subject: [PATCH] 2591. [Usaco 2012 Feb]Nearby Cows https://hydro.ac/d/bzoj/record/6322eb9032ae6c3198cc9ae8 --- BZOJ/2591/2591.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++ BZOJ/2591/data/1.in | 3 ++ BZOJ/2591/data/1.out | 3 ++ BZOJ/2591/data/10.in | 3 ++ BZOJ/2591/data/10.out | 3 ++ BZOJ/2591/data/2.in | 3 ++ BZOJ/2591/data/2.out | 3 ++ BZOJ/2591/data/3.in | 3 ++ BZOJ/2591/data/3.out | 3 ++ BZOJ/2591/data/4.in | 3 ++ BZOJ/2591/data/4.out | 3 ++ BZOJ/2591/data/5.in | 3 ++ BZOJ/2591/data/5.out | 3 ++ BZOJ/2591/data/6.in | 3 ++ BZOJ/2591/data/6.out | 3 ++ BZOJ/2591/data/7.in | 3 ++ BZOJ/2591/data/7.out | 3 ++ BZOJ/2591/data/8.in | 3 ++ BZOJ/2591/data/8.out | 3 ++ BZOJ/2591/data/9.in | 3 ++ BZOJ/2591/data/9.out | 3 ++ 21 files changed, 139 insertions(+) create mode 100644 BZOJ/2591/2591.cpp create mode 100644 BZOJ/2591/data/1.in create mode 100644 BZOJ/2591/data/1.out create mode 100644 BZOJ/2591/data/10.in create mode 100644 BZOJ/2591/data/10.out create mode 100644 BZOJ/2591/data/2.in create mode 100644 BZOJ/2591/data/2.out create mode 100644 BZOJ/2591/data/3.in create mode 100644 BZOJ/2591/data/3.out create mode 100644 BZOJ/2591/data/4.in create mode 100644 BZOJ/2591/data/4.out create mode 100644 BZOJ/2591/data/5.in create mode 100644 BZOJ/2591/data/5.out create mode 100644 BZOJ/2591/data/6.in create mode 100644 BZOJ/2591/data/6.out create mode 100644 BZOJ/2591/data/7.in create mode 100644 BZOJ/2591/data/7.out create mode 100644 BZOJ/2591/data/8.in create mode 100644 BZOJ/2591/data/8.out create mode 100644 BZOJ/2591/data/9.in create mode 100644 BZOJ/2591/data/9.out diff --git a/BZOJ/2591/2591.cpp b/BZOJ/2591/2591.cpp new file mode 100644 index 00000000..b7bef19f --- /dev/null +++ b/BZOJ/2591/2591.cpp @@ -0,0 +1,79 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 100005; + +int n, k, fa[N]; +std::vector g[N]; +std::array f1, f2; + +void dfs1(int u) { + for (int v : g[u]) { + if (v == fa[u]) continue; + + fa[v] = u; + dfs1(v); + + for (int i = 1; i <= k; i++) { + f1[u][i] += f1[v][i - 1]; + } + } +} + +void dfs2(int u) { + for (int v : g[u]) { + if (v != fa[u]) dfs2(v); + } + + for (int i = 1; i <= k; i++) { + int cnt = 0, t = u; + + while (++cnt < i && fa[t]) { + f1[u][i] += (f2[fa[t]][i - cnt] - f2[t][i - cnt - 1]); + t = fa[t]; + } + + if (fa[t]) f1[u][i] += f2[fa[t]][0]; + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> k; + + for (int i = 1, u, v; i < n; i++) { + cin >> u >> v; + + g[u].push_back(v); + g[v].push_back(u); + } + + for (int i = 1, x; i <= n; i++) { + cin >> x; + + f1[i][0] = x; + } + + dfs1(1); + f2 = f1; + dfs2(1); + + for (int i = 1; i <= n; i++) { + int sum = 0; + + for (int j = 0; j <= k; j++) { + sum += f1[i][j]; + } + + cout << sum << endl; + } + + return 0; +} diff --git a/BZOJ/2591/data/1.in b/BZOJ/2591/data/1.in new file mode 100644 index 00000000..761e7013 --- /dev/null +++ b/BZOJ/2591/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ce69a6748e72bb441a7705961be8f7c3ce7058bc957eb186a716aa90526b5e0a +size 375138 diff --git a/BZOJ/2591/data/1.out b/BZOJ/2591/data/1.out new file mode 100644 index 00000000..9d9d329c --- /dev/null +++ b/BZOJ/2591/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1cd36f84be461d9c4e4d09ebe67ee5718dc0ebe1a502bc27433577bb9689a7a4 +size 155741 diff --git a/BZOJ/2591/data/10.in b/BZOJ/2591/data/10.in new file mode 100644 index 00000000..0ccc6beb --- /dev/null +++ b/BZOJ/2591/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2b2e05fa3d25f411efcea83303aee9117ca05237dfce6ed118736de571ef58b6 +size 36 diff --git a/BZOJ/2591/data/10.out b/BZOJ/2591/data/10.out new file mode 100644 index 00000000..e1abb1e9 --- /dev/null +++ b/BZOJ/2591/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:67ffa84b61fe1ace5fe016605a36e77b9e6ff38b2ad467b15791b2b7d7b1fb2f +size 17 diff --git a/BZOJ/2591/data/2.in b/BZOJ/2591/data/2.in new file mode 100644 index 00000000..571fb62d --- /dev/null +++ b/BZOJ/2591/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cb87a33784c146d95a107ad42418ebbc411fc57983a6a598a36b9321bcf4517a +size 975 diff --git a/BZOJ/2591/data/2.out b/BZOJ/2591/data/2.out new file mode 100644 index 00000000..3f407b88 --- /dev/null +++ b/BZOJ/2591/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:24f885f7634a04883e16144a40e320778be8791ec2a2944b2f1f94556ea340f9 +size 600 diff --git a/BZOJ/2591/data/3.in b/BZOJ/2591/data/3.in new file mode 100644 index 00000000..bce753f2 --- /dev/null +++ b/BZOJ/2591/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8e02ccf3f46b147b7a6c8a1fdc635c5317ee895d7d0c1c29cc354eec91d4b6b8 +size 1566924 diff --git a/BZOJ/2591/data/3.out b/BZOJ/2591/data/3.out new file mode 100644 index 00000000..78dd224c --- /dev/null +++ b/BZOJ/2591/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d9132b5d0e8e72d51fdd6b6454790e18d0791dac9bd4f303c34faeaeaa57283e +size 626738 diff --git a/BZOJ/2591/data/4.in b/BZOJ/2591/data/4.in new file mode 100644 index 00000000..ac7fc069 --- /dev/null +++ b/BZOJ/2591/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4939685d1987358ded396c06bc1ac0c60ce37663c06ac7ecea7ae09973701a41 +size 1567008 diff --git a/BZOJ/2591/data/4.out b/BZOJ/2591/data/4.out new file mode 100644 index 00000000..1b82824d --- /dev/null +++ b/BZOJ/2591/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e16daafde2492989cf7845cd1aa5acf3034ce2f56d19ff5b0a0319c9a3f40623 +size 600000 diff --git a/BZOJ/2591/data/5.in b/BZOJ/2591/data/5.in new file mode 100644 index 00000000..e3b17e5e --- /dev/null +++ b/BZOJ/2591/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6ce0d3005c471c4e9f69e8db975a39f54902c404c869b7d9d720285349394e2e +size 1566928 diff --git a/BZOJ/2591/data/5.out b/BZOJ/2591/data/5.out new file mode 100644 index 00000000..2e234bf2 --- /dev/null +++ b/BZOJ/2591/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f1b49582cc3db8a6997259c76d79ed3231719b2ebd9258ff39e1b15f80e59986 +size 889992 diff --git a/BZOJ/2591/data/6.in b/BZOJ/2591/data/6.in new file mode 100644 index 00000000..c37e4f9e --- /dev/null +++ b/BZOJ/2591/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4fce92150ca25e22498d440fb3cb3a7f48465acf8efcc81d952a4b3e833f5263 +size 6 diff --git a/BZOJ/2591/data/6.out b/BZOJ/2591/data/6.out new file mode 100644 index 00000000..f2baf931 --- /dev/null +++ b/BZOJ/2591/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:10159baf262b43a92d95db59dae1f72c645127301661e0a3ce4e38b295a97c58 +size 2 diff --git a/BZOJ/2591/data/7.in b/BZOJ/2591/data/7.in new file mode 100644 index 00000000..174638a7 --- /dev/null +++ b/BZOJ/2591/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4f258941ca2697795b47145843b78f39f7d4396a9051ea9ed0df3f8cd2cb929d +size 772214 diff --git a/BZOJ/2591/data/7.out b/BZOJ/2591/data/7.out new file mode 100644 index 00000000..91ae3482 --- /dev/null +++ b/BZOJ/2591/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2fb7d27a7145c0e20bdc4de448262fa4e52773e724641b4a5fd02e5aaed9de6b +size 437020 diff --git a/BZOJ/2591/data/8.in b/BZOJ/2591/data/8.in new file mode 100644 index 00000000..69139544 --- /dev/null +++ b/BZOJ/2591/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ab59a5215f6f6487b5b6de99b2c63d3c889bcf9b6e730333d22e72fb0a222ad4 +size 67304 diff --git a/BZOJ/2591/data/8.out b/BZOJ/2591/data/8.out new file mode 100644 index 00000000..35aae64f --- /dev/null +++ b/BZOJ/2591/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d5f185d8b4c518ec356ffcceb0c20f43fb4749595a7bb17de0cfe02a7e24682 +size 31155 diff --git a/BZOJ/2591/data/9.in b/BZOJ/2591/data/9.in new file mode 100644 index 00000000..de9fde5e --- /dev/null +++ b/BZOJ/2591/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1b1e3119866387ebf5b2c6bf6ff0034dbbbfa7188ede28fac5f4e7dce0f66f74 +size 1577917 diff --git a/BZOJ/2591/data/9.out b/BZOJ/2591/data/9.out new file mode 100644 index 00000000..0c07b6bc --- /dev/null +++ b/BZOJ/2591/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f1b30cfbac87577fa5709e831badde13ccd13f737d443e579937f4f61fe3a5b5 +size 900000