From bfd9d2d331c1c782c6f19af2ee47b424629f61d0 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 21 Dec 2022 20:18:57 +0800 Subject: [PATCH] =?UTF-8?q?#1802.=20=E3=80=902017.3=20=E9=95=BF=E4=B9=90?= =?UTF-8?q?=E7=9C=81=E9=80=89=E9=9B=86=E8=AE=AD=20Day7=20T1=E3=80=91?= =?UTF-8?q?=E9=87=87=E8=98=91=E8=8F=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/66112 --- S2OJ/1802/1802.cpp | 90 +++++++++++++++++++++++++++++++++++ S2OJ/1802/data/mushroom1.in | 3 ++ S2OJ/1802/data/mushroom1.out | 3 ++ S2OJ/1802/data/mushroom10.in | 3 ++ S2OJ/1802/data/mushroom10.out | 3 ++ S2OJ/1802/data/mushroom2.in | 3 ++ S2OJ/1802/data/mushroom2.out | 3 ++ S2OJ/1802/data/mushroom3.in | 3 ++ S2OJ/1802/data/mushroom3.out | 3 ++ S2OJ/1802/data/mushroom4.in | 3 ++ S2OJ/1802/data/mushroom4.out | 3 ++ S2OJ/1802/data/mushroom5.in | 3 ++ S2OJ/1802/data/mushroom5.out | 3 ++ S2OJ/1802/data/mushroom6.in | 3 ++ S2OJ/1802/data/mushroom6.out | 3 ++ S2OJ/1802/data/mushroom7.in | 3 ++ S2OJ/1802/data/mushroom7.out | 3 ++ S2OJ/1802/data/mushroom8.in | 3 ++ S2OJ/1802/data/mushroom8.out | 3 ++ S2OJ/1802/data/mushroom9.in | 3 ++ S2OJ/1802/data/mushroom9.out | 3 ++ S2OJ/1802/data/problem.conf | 3 ++ 22 files changed, 153 insertions(+) create mode 100644 S2OJ/1802/1802.cpp create mode 100644 S2OJ/1802/data/mushroom1.in create mode 100644 S2OJ/1802/data/mushroom1.out create mode 100644 S2OJ/1802/data/mushroom10.in create mode 100644 S2OJ/1802/data/mushroom10.out create mode 100644 S2OJ/1802/data/mushroom2.in create mode 100644 S2OJ/1802/data/mushroom2.out create mode 100644 S2OJ/1802/data/mushroom3.in create mode 100644 S2OJ/1802/data/mushroom3.out create mode 100644 S2OJ/1802/data/mushroom4.in create mode 100644 S2OJ/1802/data/mushroom4.out create mode 100644 S2OJ/1802/data/mushroom5.in create mode 100644 S2OJ/1802/data/mushroom5.out create mode 100644 S2OJ/1802/data/mushroom6.in create mode 100644 S2OJ/1802/data/mushroom6.out create mode 100644 S2OJ/1802/data/mushroom7.in create mode 100644 S2OJ/1802/data/mushroom7.out create mode 100644 S2OJ/1802/data/mushroom8.in create mode 100644 S2OJ/1802/data/mushroom8.out create mode 100644 S2OJ/1802/data/mushroom9.in create mode 100644 S2OJ/1802/data/mushroom9.out create mode 100644 S2OJ/1802/data/problem.conf diff --git a/S2OJ/1802/1802.cpp b/S2OJ/1802/1802.cpp new file mode 100644 index 00000000..45c9a697 --- /dev/null +++ b/S2OJ/1802/1802.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3e5 + 5; + +int n, m, c[N], cnt, dfn[N], siz[N], col_siz[N], tag[N], ans[N]; +std::vector g[N], col[N]; +std::unordered_set set; + +void dfs1(int u, int f) { + dfn[u] = ++cnt; + siz[u] = 1; + + for (int v : g[u]) { + if (v == f) continue; + + int t = col_siz[c[u]]; + + dfs1(v, u); + + siz[u] += siz[v]; + + t = siz[v] - (col_siz[c[u]] - t); + + col_siz[c[u]] += t; + tag[v] += t; + + while (!col[c[u]].empty() && dfn[col[c[u]].back()] > dfn[u]) { + tag[col[c[u]].back()] -= t; + col[c[u]].pop_back(); + } + } + + col_siz[c[u]]++; + col[c[u]].emplace_back(u); +} + +void dfs2(int u, int f) { + ans[u] = ans[f] + tag[u]; + + for (int v : g[u]) { + if (v == f) continue; + + 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]; + + m = std::max(m, c[i]); + set.emplace(c[i]); + } + + for (int i = 1, u, v; i < n; i++) { + cin >> u >> v; + + g[u].emplace_back(v); + g[v].emplace_back(u); + } + + dfs1(1, 0); + + for (int x : set) { + tag[1] += n - col_siz[x]; + + for (int v : col[x]) { + tag[v] -= n - col_siz[x]; + } + } + + dfs2(1, 0); + + for (int i = 1; i <= n; i++) { + cout << n * set.size() - ans[i] << endl; + } + + return 0; +} diff --git a/S2OJ/1802/data/mushroom1.in b/S2OJ/1802/data/mushroom1.in new file mode 100644 index 00000000..2c74c33d --- /dev/null +++ b/S2OJ/1802/data/mushroom1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:12093c0ae501787d36d35fe664149895f7da5b08d47879c58f40ec6da1156ed2 +size 26647 diff --git a/S2OJ/1802/data/mushroom1.out b/S2OJ/1802/data/mushroom1.out new file mode 100644 index 00000000..f4ceaf7a --- /dev/null +++ b/S2OJ/1802/data/mushroom1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:87274aaff13e8754e2449ba9508be7448eb6bf7e9c1465dbcded97c7b1578e51 +size 13321 diff --git a/S2OJ/1802/data/mushroom10.in b/S2OJ/1802/data/mushroom10.in new file mode 100644 index 00000000..5473d583 --- /dev/null +++ b/S2OJ/1802/data/mushroom10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7ab861fcac68b899c501bff1c7478352aa889f3ecd5bf93c0cf038ed30e5cb2a +size 5675111 diff --git a/S2OJ/1802/data/mushroom10.out b/S2OJ/1802/data/mushroom10.out new file mode 100644 index 00000000..33ba69b4 --- /dev/null +++ b/S2OJ/1802/data/mushroom10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bbc66d90d9a99dacddeda9d09025fd3e3d6e2a855cf91c96c2ac4fb52c3893f2 +size 3000000 diff --git a/S2OJ/1802/data/mushroom2.in b/S2OJ/1802/data/mushroom2.in new file mode 100644 index 00000000..8c02d669 --- /dev/null +++ b/S2OJ/1802/data/mushroom2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9fb9c2305f7023937314736bd882b424fc36dcc70b9ce8a75b4b3dbbc9a2208e +size 26817 diff --git a/S2OJ/1802/data/mushroom2.out b/S2OJ/1802/data/mushroom2.out new file mode 100644 index 00000000..aee9a816 --- /dev/null +++ b/S2OJ/1802/data/mushroom2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:952e466b25187324569f5151eaee5ad46ea995dd61516c4bfd93e7b71a72e64c +size 12971 diff --git a/S2OJ/1802/data/mushroom3.in b/S2OJ/1802/data/mushroom3.in new file mode 100644 index 00000000..6fcfebad --- /dev/null +++ b/S2OJ/1802/data/mushroom3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f0a21c8765610d59a8f0bc60dc40cefc23b7180c307e29badacf07feb614b21b +size 26743 diff --git a/S2OJ/1802/data/mushroom3.out b/S2OJ/1802/data/mushroom3.out new file mode 100644 index 00000000..7aae973d --- /dev/null +++ b/S2OJ/1802/data/mushroom3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:845a58167a294eac312d6453cb6ce78d9c44aa5b41a28e004fc1fcce7a682dae +size 12806 diff --git a/S2OJ/1802/data/mushroom4.in b/S2OJ/1802/data/mushroom4.in new file mode 100644 index 00000000..e7fcc807 --- /dev/null +++ b/S2OJ/1802/data/mushroom4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5981476de329e2fd3d343f984d28370cd156d1eefab7833530d176e7cdfcba37 +size 5684340 diff --git a/S2OJ/1802/data/mushroom4.out b/S2OJ/1802/data/mushroom4.out new file mode 100644 index 00000000..9f0af406 --- /dev/null +++ b/S2OJ/1802/data/mushroom4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:523f94b718328078c28e392fd697d1639f25863cd8c04b1e58b77ff4a17daee6 +size 3000000 diff --git a/S2OJ/1802/data/mushroom5.in b/S2OJ/1802/data/mushroom5.in new file mode 100644 index 00000000..a0414fb7 --- /dev/null +++ b/S2OJ/1802/data/mushroom5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1a7c7550abf6c9b1f9b16e67a7a4d017edbad77261f1a3f0701cab3e1e5bab18 +size 5681245 diff --git a/S2OJ/1802/data/mushroom5.out b/S2OJ/1802/data/mushroom5.out new file mode 100644 index 00000000..443c93a8 --- /dev/null +++ b/S2OJ/1802/data/mushroom5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2769fec7e0c73d281e6d73f50d0a94eb63f0b0c4f9634469283069f21d3132ba +size 3000000 diff --git a/S2OJ/1802/data/mushroom6.in b/S2OJ/1802/data/mushroom6.in new file mode 100644 index 00000000..cd240408 --- /dev/null +++ b/S2OJ/1802/data/mushroom6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bc9ce8ee3b64d39de4c50b909d6fe27765a1c1670e87603ee3a92aba0093db7c +size 5677347 diff --git a/S2OJ/1802/data/mushroom6.out b/S2OJ/1802/data/mushroom6.out new file mode 100644 index 00000000..076e447c --- /dev/null +++ b/S2OJ/1802/data/mushroom6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3acc47defe75db5c3a6c932c68af83c9ae90f3a4cd7064513580105a85525990 +size 2100000 diff --git a/S2OJ/1802/data/mushroom7.in b/S2OJ/1802/data/mushroom7.in new file mode 100644 index 00000000..37db1afc --- /dev/null +++ b/S2OJ/1802/data/mushroom7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8d65745870202a438cd22e548faa33da6ce038b2b263d8178d6ea470ba57d83d +size 5677954 diff --git a/S2OJ/1802/data/mushroom7.out b/S2OJ/1802/data/mushroom7.out new file mode 100644 index 00000000..c2968361 --- /dev/null +++ b/S2OJ/1802/data/mushroom7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:51f267aad37e966d0e1b4252e5e627ba341668ac8dfc8862a18aba1765542342 +size 2100000 diff --git a/S2OJ/1802/data/mushroom8.in b/S2OJ/1802/data/mushroom8.in new file mode 100644 index 00000000..f47d7a8d --- /dev/null +++ b/S2OJ/1802/data/mushroom8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f02185a32076802608acb27d06267d7dc9e57fa4ca4dd30727ce2d0bff6d940d +size 5677612 diff --git a/S2OJ/1802/data/mushroom8.out b/S2OJ/1802/data/mushroom8.out new file mode 100644 index 00000000..be5698dc --- /dev/null +++ b/S2OJ/1802/data/mushroom8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6fbd5cb40731cb56691527e214302eb4c3a93372561812b383b21f74c2c3834b +size 3000000 diff --git a/S2OJ/1802/data/mushroom9.in b/S2OJ/1802/data/mushroom9.in new file mode 100644 index 00000000..6ae7e535 --- /dev/null +++ b/S2OJ/1802/data/mushroom9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fdb46f604fbb1d1d421bc053aa56e9c89cdea3596a4a1aac4e6af95723b08107 +size 5678171 diff --git a/S2OJ/1802/data/mushroom9.out b/S2OJ/1802/data/mushroom9.out new file mode 100644 index 00000000..dd59a713 --- /dev/null +++ b/S2OJ/1802/data/mushroom9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c703f84f3c0b25fd2608696409ff20be4265925e93e45b0fce670d81d5d32b55 +size 3000000 diff --git a/S2OJ/1802/data/problem.conf b/S2OJ/1802/data/problem.conf new file mode 100644 index 00000000..ac3d7e06 --- /dev/null +++ b/S2OJ/1802/data/problem.conf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1855a83e07ff30ce9570551aa5a62d978ee6ca811b7a708c96f9d02dde1b9f5b +size 185