diff --git a/S2OJ/1822/1822.cpp b/S2OJ/1822/1822.cpp new file mode 100644 index 00000000..db497beb --- /dev/null +++ b/S2OJ/1822/1822.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n, a[N], c[N], siz[N], max[N], + _cnt[N << 1], *cnt(_cnt + N), pos[N], sum[N], root, ans; +std::vector g[N]; +bool vis[N]; + +void get_root(int u, int f, int s) { + siz[u] = 1; + max[u] = 0; + + for (auto v : g[u]) { + if (v == f || vis[v]) continue; + + get_root(v, u, s); + + siz[u] += siz[v]; + max[u] = std::max(max[u], siz[v]); + } + + max[u] = std::max(max[u], s - siz[u]); + + if (!root || max[u] < max[root]) { + root = u; + } +} + +void add(int u, int f, int d) { + cnt[d]++; + + for (int v : g[u]) { + if (v == f || vis[v]) continue; + + add(v, u, d + c[v]); + } +} + +void del(int u, int f, int d) { + cnt[d]--; + + for (int v : g[u]) { + if (v == f || vis[v]) continue; + + del(v, u, d + c[v]); + } +} + +void calc(int u, int f, int d) { + sum[pos[u]] += cnt[-d]; + + for (int v : g[u]) { + if (v == f || vis[v]) continue; + + calc(v, u, d + c[v]); + } +} + +void solve(int u) { + vis[u] = true; + cnt[0]++; + + for (int v : g[u]) { + if (vis[v]) continue; + + calc(v, u, c[u] + c[v]); + add(v, u, c[v]); + } + + sum[pos[u]] += cnt[-c[u]]; + cnt[0]--; + + for (int v : g[u]) { + if (vis[v]) continue; + + del(v, u, c[v]); + } + + std::for_each(g[u].rbegin(), g[u].rend(), [&](int v) -> void { + if (vis[v]) return; + + calc(v, u, c[u] + c[v]); + add(v, u, c[v]); + }); + + for (int v : g[u]) { + if (vis[v]) continue; + + del(v, u, c[v]); + } + + for (int v : g[u]) { + if (vis[v]) continue; + + root = 0; + get_root(v, 0, siz[v]); + get_root(root, 0, siz[v]); + solve(root); + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1, x; i <= n; i++) { + cin >> x; + + c[i] = x ? 1 : -1; + } + + for (int i = 1, x, y; i < n; i++) { + cin >> x >> y; + + g[x].emplace_back(y); + g[y].emplace_back(x); + } + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + pos[a[i]] = i; + } + + get_root(1, 0, n); + get_root(root, 0, n); + solve(root); + + std::partial_sum(sum + 1, sum + 1 + n, sum + 1); + + for (int l = 1, r = 1; l <= n; l++) { + while (r <= n && sum[r] - sum[l - 1] <= sum[n] - sum[r] + sum[l - 1]) r++; + + ans += n - r + 1; + } + + cout << ans << endl; + + return 0; +} diff --git a/S2OJ/1822/data/input1.in b/S2OJ/1822/data/input1.in new file mode 100644 index 00000000..076dcacc --- /dev/null +++ b/S2OJ/1822/data/input1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a5934aaa0a4dfad269aba2088880a984607ae1eb1ac0ea043fc775eff618df06 +size 13480 diff --git a/S2OJ/1822/data/input1.out b/S2OJ/1822/data/input1.out new file mode 100644 index 00000000..ad53516b --- /dev/null +++ b/S2OJ/1822/data/input1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7d92296d32aaaf0ec06ef58bbebbe0bf641340df1bf38bb94532fa8caa7487af +size 7 diff --git a/S2OJ/1822/data/input10.in b/S2OJ/1822/data/input10.in new file mode 100644 index 00000000..016dea4f --- /dev/null +++ b/S2OJ/1822/data/input10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b36730e562605f01ec638d165c09422fec332b3d325e382de054f6bf74529aac +size 1054 diff --git a/S2OJ/1822/data/input10.out b/S2OJ/1822/data/input10.out new file mode 100644 index 00000000..b3eaa769 --- /dev/null +++ b/S2OJ/1822/data/input10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e0f5331fa120b0b3713d86affba962887891e9ad1350cf210f46b9ea8d83798d +size 5 diff --git a/S2OJ/1822/data/input2.in b/S2OJ/1822/data/input2.in new file mode 100644 index 00000000..076dcacc --- /dev/null +++ b/S2OJ/1822/data/input2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a5934aaa0a4dfad269aba2088880a984607ae1eb1ac0ea043fc775eff618df06 +size 13480 diff --git a/S2OJ/1822/data/input2.out b/S2OJ/1822/data/input2.out new file mode 100644 index 00000000..ad53516b --- /dev/null +++ b/S2OJ/1822/data/input2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7d92296d32aaaf0ec06ef58bbebbe0bf641340df1bf38bb94532fa8caa7487af +size 7 diff --git a/S2OJ/1822/data/input3.in b/S2OJ/1822/data/input3.in new file mode 100644 index 00000000..468640dd --- /dev/null +++ b/S2OJ/1822/data/input3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:34fb914576d32650f43f65ddc907426dd33ed7a965cd57a13b0b5fdd8baf4568 +size 164651 diff --git a/S2OJ/1822/data/input3.out b/S2OJ/1822/data/input3.out new file mode 100644 index 00000000..55c340f1 --- /dev/null +++ b/S2OJ/1822/data/input3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7562fd3f544ed94bbe2e5372e40449aeef633855a27675a0da4317bb9d6c71e7 +size 9 diff --git a/S2OJ/1822/data/input4.in b/S2OJ/1822/data/input4.in new file mode 100644 index 00000000..cda69c5e --- /dev/null +++ b/S2OJ/1822/data/input4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b4294703b9f1ad5085853d1db7c650d328509446b4ae18a30c42856b841635a4 +size 164571 diff --git a/S2OJ/1822/data/input4.out b/S2OJ/1822/data/input4.out new file mode 100644 index 00000000..2f5e2429 --- /dev/null +++ b/S2OJ/1822/data/input4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:75c7d58e551b3109e840771d45f8e915aa0276edf565e1cec1cde57f23b4eb4e +size 9 diff --git a/S2OJ/1822/data/input5.in b/S2OJ/1822/data/input5.in new file mode 100644 index 00000000..cda69c5e --- /dev/null +++ b/S2OJ/1822/data/input5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b4294703b9f1ad5085853d1db7c650d328509446b4ae18a30c42856b841635a4 +size 164571 diff --git a/S2OJ/1822/data/input5.out b/S2OJ/1822/data/input5.out new file mode 100644 index 00000000..2f5e2429 --- /dev/null +++ b/S2OJ/1822/data/input5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:75c7d58e551b3109e840771d45f8e915aa0276edf565e1cec1cde57f23b4eb4e +size 9 diff --git a/S2OJ/1822/data/input6.in b/S2OJ/1822/data/input6.in new file mode 100644 index 00000000..a9aae6f3 --- /dev/null +++ b/S2OJ/1822/data/input6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:252c2fcfd0a5a00bc25d7f4081d0a7387d6513c7f1ff844f32945d2f9a379ed2 +size 1942640 diff --git a/S2OJ/1822/data/input6.out b/S2OJ/1822/data/input6.out new file mode 100644 index 00000000..f9d20b36 --- /dev/null +++ b/S2OJ/1822/data/input6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:38bd0fad3afabf2dc7d06d7b23477b3046889dceacfe84ceb2b876367fe13953 +size 11 diff --git a/S2OJ/1822/data/input7.in b/S2OJ/1822/data/input7.in new file mode 100644 index 00000000..034eb24f --- /dev/null +++ b/S2OJ/1822/data/input7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:422656b650411df3d37b3f73886251b138ea5d2fa85fe2f83c3ea3f2ad69a2e3 +size 1942413 diff --git a/S2OJ/1822/data/input7.out b/S2OJ/1822/data/input7.out new file mode 100644 index 00000000..82311b56 --- /dev/null +++ b/S2OJ/1822/data/input7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15b0a0684b0da1988f8079c8ab218b55f224f48b97e1e59d484b60995e0cde15 +size 11 diff --git a/S2OJ/1822/data/input8.in b/S2OJ/1822/data/input8.in new file mode 100644 index 00000000..19fdba41 --- /dev/null +++ b/S2OJ/1822/data/input8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:69f85e2455c82fd89cf0298a63301d15e1d06ee380c82fbfca7d762f33be9776 +size 1942393 diff --git a/S2OJ/1822/data/input8.out b/S2OJ/1822/data/input8.out new file mode 100644 index 00000000..9f068628 --- /dev/null +++ b/S2OJ/1822/data/input8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1488986c80d02bd1e92d50de42bf4f1fb7d3eb3cf366473cf2dfc64f05d6b741 +size 11 diff --git a/S2OJ/1822/data/input9.in b/S2OJ/1822/data/input9.in new file mode 100644 index 00000000..79a5041a --- /dev/null +++ b/S2OJ/1822/data/input9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d2199df0af2c768bf5422a22ee15c55ce2a75c0188463baacf20acdd4459b0a +size 1942538 diff --git a/S2OJ/1822/data/input9.out b/S2OJ/1822/data/input9.out new file mode 100644 index 00000000..7f3d9554 --- /dev/null +++ b/S2OJ/1822/data/input9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f753709e002e07de3be359c6140b3fd30e289cfc5911051f3c4a0c1a82f777d9 +size 11 diff --git a/S2OJ/1822/data/problem.conf b/S2OJ/1822/data/problem.conf new file mode 100644 index 00000000..1b00f523 --- /dev/null +++ b/S2OJ/1822/data/problem.conf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b3c395dc837848868dc7cdd830da1c8e347ab868a593c57b10cbc1ffa078b047 +size 179