From 0ef4b0e96ccb427a6a072b13a22fff00702b208c Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 15 Sep 2022 12:48:04 +0800 Subject: [PATCH] =?UTF-8?q?1827.=20[Usaco2010=20Mar]gather=20=E5=A5=B6?= =?UTF-8?q?=E7=89=9B=E5=A4=A7=E9=9B=86=E4=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/bzoj/record/6322ae5fbdf9bc31d1606279 --- BZOJ/1827/1827.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++ BZOJ/1827/data/1.in | 3 ++ BZOJ/1827/data/1.out | 3 ++ BZOJ/1827/data/10.in | 3 ++ BZOJ/1827/data/10.out | 3 ++ BZOJ/1827/data/2.in | 3 ++ BZOJ/1827/data/2.out | 3 ++ BZOJ/1827/data/3.in | 3 ++ BZOJ/1827/data/3.out | 3 ++ BZOJ/1827/data/4.in | 3 ++ BZOJ/1827/data/4.out | 3 ++ BZOJ/1827/data/5.in | 3 ++ BZOJ/1827/data/5.out | 3 ++ BZOJ/1827/data/6.in | 3 ++ BZOJ/1827/data/6.out | 3 ++ BZOJ/1827/data/7.in | 3 ++ BZOJ/1827/data/7.out | 3 ++ BZOJ/1827/data/8.in | 3 ++ BZOJ/1827/data/8.out | 3 ++ BZOJ/1827/data/9.in | 3 ++ BZOJ/1827/data/9.out | 3 ++ 21 files changed, 141 insertions(+) create mode 100644 BZOJ/1827/1827.cpp create mode 100644 BZOJ/1827/data/1.in create mode 100644 BZOJ/1827/data/1.out create mode 100644 BZOJ/1827/data/10.in create mode 100644 BZOJ/1827/data/10.out create mode 100644 BZOJ/1827/data/2.in create mode 100644 BZOJ/1827/data/2.out create mode 100644 BZOJ/1827/data/3.in create mode 100644 BZOJ/1827/data/3.out create mode 100644 BZOJ/1827/data/4.in create mode 100644 BZOJ/1827/data/4.out create mode 100644 BZOJ/1827/data/5.in create mode 100644 BZOJ/1827/data/5.out create mode 100644 BZOJ/1827/data/6.in create mode 100644 BZOJ/1827/data/6.out create mode 100644 BZOJ/1827/data/7.in create mode 100644 BZOJ/1827/data/7.out create mode 100644 BZOJ/1827/data/8.in create mode 100644 BZOJ/1827/data/8.out create mode 100644 BZOJ/1827/data/9.in create mode 100644 BZOJ/1827/data/9.out diff --git a/BZOJ/1827/1827.cpp b/BZOJ/1827/1827.cpp new file mode 100644 index 00000000..b6bed22d --- /dev/null +++ b/BZOJ/1827/1827.cpp @@ -0,0 +1,81 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n, sum, root, c[N], siz[N], max_siz[N], dep[N]; +long long ans; +std::vector> g[N]; + +void dfs1(int u, int f) { + siz[u] = c[u]; + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (v == f) continue; + + dfs1(v, u); + + siz[u] += siz[v]; + max_siz[u] = std::max(max_siz[u], siz[v]); + } + + max_siz[u] = std::max(max_siz[u], sum - siz[u]); +} + +void dfs2(int u, int f) { + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (v == f) continue; + + dep[v] = dep[u] + w; + dfs2(v, u); + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> c[i]; + + sum += c[i]; + } + + for (int i = 1, u, v, w; i <= n; i++) { + cin >> u >> v >> w; + + g[u].emplace_back(v, w); + g[v].emplace_back(u, w); + } + + dfs1(1, 0); + + for (int i = 1; i <= n; i++) { + if (!root || max_siz[i] < max_siz[root]) { + root = i; + } + } + + dep[0] = -1; + dfs2(root, 0); + + for (int i = 1; i <= n; i++) { + ans += static_cast(c[i]) * dep[i]; + } + + cout << ans << endl; + + return 0; +} diff --git a/BZOJ/1827/data/1.in b/BZOJ/1827/data/1.in new file mode 100644 index 00000000..3aef6110 --- /dev/null +++ b/BZOJ/1827/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bd869079ca2802c71cdbfadd4357f873f5066d5477311c9b62a0402800269507 +size 977778 diff --git a/BZOJ/1827/data/1.out b/BZOJ/1827/data/1.out new file mode 100644 index 00000000..cd5ac069 --- /dev/null +++ b/BZOJ/1827/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6e822b5cf5ddb29e21d415d18e3ff8b0b640cb0f1d44f958567a65ec70019176 +size 15 diff --git a/BZOJ/1827/data/10.in b/BZOJ/1827/data/10.in new file mode 100644 index 00000000..c9feee5c --- /dev/null +++ b/BZOJ/1827/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1d95b14e985c40d3155ecbf6c647da114b686c99fd88cc4fea623b332d4c3d62 +size 196 diff --git a/BZOJ/1827/data/10.out b/BZOJ/1827/data/10.out new file mode 100644 index 00000000..83f1a1d7 --- /dev/null +++ b/BZOJ/1827/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a9339b0f71271bb2bb62890f22244ed0468fdecbcb8a57cc739e25619dfad25e +size 7 diff --git a/BZOJ/1827/data/2.in b/BZOJ/1827/data/2.in new file mode 100644 index 00000000..253962bf --- /dev/null +++ b/BZOJ/1827/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7ac0cb8ab7c93019eeee10b12125dad69f6c5dd04abfd0161a9a3b19206ba862 +size 1568116 diff --git a/BZOJ/1827/data/2.out b/BZOJ/1827/data/2.out new file mode 100644 index 00000000..e12fcb0f --- /dev/null +++ b/BZOJ/1827/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6630030fa8f9449f850c825ca3c84961297508e353586043ca699b19c634b1d4 +size 9 diff --git a/BZOJ/1827/data/3.in b/BZOJ/1827/data/3.in new file mode 100644 index 00000000..a573f1ab --- /dev/null +++ b/BZOJ/1827/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:495e298c8297b6b63a7c517229956e567dd10c401004f53ba7611689c12b2ba1 +size 949297 diff --git a/BZOJ/1827/data/3.out b/BZOJ/1827/data/3.out new file mode 100644 index 00000000..00d45b55 --- /dev/null +++ b/BZOJ/1827/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:69339bdca1b15a81a07f2182f93ba99c6c0790a10d042fde7ba583746769c328 +size 12 diff --git a/BZOJ/1827/data/4.in b/BZOJ/1827/data/4.in new file mode 100644 index 00000000..17d3e4fa --- /dev/null +++ b/BZOJ/1827/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d6644f912434fc7024e4ba5ddc107a6bdfccd76d7abe65dcd4703bc02ed1fd9b +size 440550 diff --git a/BZOJ/1827/data/4.out b/BZOJ/1827/data/4.out new file mode 100644 index 00000000..01200f73 --- /dev/null +++ b/BZOJ/1827/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:46629f7e9631707459325de773385cc965f46a1ea4312a6d559d0c230a12e5fd +size 10 diff --git a/BZOJ/1827/data/5.in b/BZOJ/1827/data/5.in new file mode 100644 index 00000000..00911d63 --- /dev/null +++ b/BZOJ/1827/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cd5df90d3a23a2818d409692a11afd5dfd909feade6707317a5d8eecd3f11f9e +size 79399 diff --git a/BZOJ/1827/data/5.out b/BZOJ/1827/data/5.out new file mode 100644 index 00000000..2999ab17 --- /dev/null +++ b/BZOJ/1827/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:abc40976fd4b1110955bd24615a3641457878cfeb0c94ba435ba88f808b71de1 +size 11 diff --git a/BZOJ/1827/data/6.in b/BZOJ/1827/data/6.in new file mode 100644 index 00000000..6abe55cf --- /dev/null +++ b/BZOJ/1827/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e6d6ef9c3204536a12a189f2c58c542a59d12e703b6b61876ef7aefd65a69bec +size 47 diff --git a/BZOJ/1827/data/6.out b/BZOJ/1827/data/6.out new file mode 100644 index 00000000..a3632bd1 --- /dev/null +++ b/BZOJ/1827/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:848c72d3b873b82f20139b86563f3542fa54c3f6c10205cc6cd87a2a6055845f +size 6 diff --git a/BZOJ/1827/data/7.in b/BZOJ/1827/data/7.in new file mode 100644 index 00000000..7be5fbc9 --- /dev/null +++ b/BZOJ/1827/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5e5a33cb125f541434393c6a406e51fc0019c3611af3cbf5321007bab685f9ed +size 40477 diff --git a/BZOJ/1827/data/7.out b/BZOJ/1827/data/7.out new file mode 100644 index 00000000..2b0f58fd --- /dev/null +++ b/BZOJ/1827/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c75e40934d3e5229380a5f9c0c616659f889816294483402edcf9144a071c665 +size 10 diff --git a/BZOJ/1827/data/8.in b/BZOJ/1827/data/8.in new file mode 100644 index 00000000..33ec9097 --- /dev/null +++ b/BZOJ/1827/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:09bddae6575426ba09090e7d7170d074ae597b687a113f99f433dc7b90dd07bb +size 36 diff --git a/BZOJ/1827/data/8.out b/BZOJ/1827/data/8.out new file mode 100644 index 00000000..2ccf849a --- /dev/null +++ b/BZOJ/1827/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:238903180cc104ec2c5d8b3f20c5bc61b389ec0a967df8cc208cdc7cd454174f +size 3 diff --git a/BZOJ/1827/data/9.in b/BZOJ/1827/data/9.in new file mode 100644 index 00000000..43c778d6 --- /dev/null +++ b/BZOJ/1827/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1f27616421d083e68ad69a20ec600e5a7620ebf8c107cc9fe572e1e3b4fd1d4b +size 1949211 diff --git a/BZOJ/1827/data/9.out b/BZOJ/1827/data/9.out new file mode 100644 index 00000000..e2b810dd --- /dev/null +++ b/BZOJ/1827/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d2a221dc5b3e8053d1c3ed681f260fa4fc25f309bdf36b97e7e61368fc588d71 +size 13