結果

問題 No.2892 Lime and Karin
ユーザー noiminoimi
提出日時 2024-09-13 23:53:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 8,000 ms
コード長 3,537 bytes
コンパイル時間 2,703 ms
コンパイル使用メモリ 211,760 KB
実行使用メモリ 26,240 KB
最終ジャッジ日時 2024-09-13 23:53:26
合計ジャッジ時間 7,643 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 44 ms
7,936 KB
testcase_15 AC 40 ms
7,552 KB
testcase_16 AC 72 ms
10,368 KB
testcase_17 AC 16 ms
6,940 KB
testcase_18 AC 69 ms
10,368 KB
testcase_19 AC 122 ms
12,416 KB
testcase_20 AC 6 ms
6,944 KB
testcase_21 AC 50 ms
8,704 KB
testcase_22 AC 71 ms
10,112 KB
testcase_23 AC 28 ms
6,940 KB
testcase_24 AC 107 ms
12,544 KB
testcase_25 AC 115 ms
12,628 KB
testcase_26 AC 110 ms
12,672 KB
testcase_27 AC 108 ms
12,672 KB
testcase_28 AC 125 ms
12,672 KB
testcase_29 AC 119 ms
12,672 KB
testcase_30 AC 108 ms
12,588 KB
testcase_31 AC 119 ms
12,672 KB
testcase_32 AC 114 ms
12,544 KB
testcase_33 AC 109 ms
12,636 KB
testcase_34 AC 45 ms
13,204 KB
testcase_35 AC 45 ms
13,068 KB
testcase_36 AC 47 ms
13,200 KB
testcase_37 AC 47 ms
13,196 KB
testcase_38 AC 47 ms
13,076 KB
testcase_39 AC 82 ms
19,732 KB
testcase_40 AC 81 ms
25,600 KB
testcase_41 AC 85 ms
20,680 KB
testcase_42 AC 84 ms
26,240 KB
testcase_43 AC 90 ms
22,432 KB
testcase_44 AC 36 ms
7,040 KB
testcase_45 AC 96 ms
11,776 KB
testcase_46 AC 41 ms
7,680 KB
testcase_47 AC 53 ms
8,960 KB
testcase_48 AC 23 ms
6,940 KB
testcase_49 AC 68 ms
9,856 KB
testcase_50 AC 75 ms
12,908 KB
testcase_51 AC 74 ms
12,800 KB
testcase_52 AC 71 ms
12,824 KB
testcase_53 AC 71 ms
12,832 KB
testcase_54 AC 75 ms
12,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define ov4(a, b, c, d, name, ...) name
#define rep3(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep2(i, a, b) rep3(i, a, b, 1)
#define rep1(i, n) rep2(i, 0, n)
#define rep0(n) rep1(aaaaa, n)
#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define per(i, a, b) for(ll i = (a) - 1; i >= (b); i--)
#define fore(e, v) for(auto &&e : v)
#define all(a) begin(a), end(a)
#define si(a) (int)(size(a))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define eb emplace_back

template <typename T, typename S> bool chmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; }
template <typename T, typename S> bool chmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; }

const int INF = 1e9 + 100;
const ll INFL = 3e18 + 100;

#define i128 __int128_t

struct _ {
    _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;

struct BIT {
    vi a;
    BIT(int n) : a(n + 1) {}
    void add(int i, int x) {
        i++;
        while(i < si(a)) a[i] += x, i += i & -i;
    }
    int sum(int r) {
        int s = 0;
        while(r) s += a[r], r -= r & -r;
        return s;
    }
    int sum(int l, int r) { return sum(r) - sum(l); }
};

int main() {
    int n;
    cin >> n;
    vector<vi> g(n);
    rep(n - 1) {
        int x, y;
        cin >> x >> y;
        --x, --y;
        g[x].eb(y), g[y].eb(x);
    }

    string s;
    cin >> s;
    vi a(n);
    rep(i, n) a[i] = (s[i] == '1' ? 1 : -1);

    if(n == 1) {
        cout << count(all(a), 1) << endl;
        exit(0);
    }

    vi sub(n), v(n);
    auto dfs = [&](auto &&f, int x, int p) -> void {
        v[x] += a[x];
        sub[x]++;
        if(g[x][0] == p) swap(g[x][0], g[x].back());
        fore(e, g[x]) {
            if(e == p) continue;
            v[e] = v[x];
            f(f, e, x);
            sub[x] += sub[e];
            if(sub[e] > sub[g[x][0]]) swap(e, g[x][0]);
        }
        return;
    };
    dfs(dfs, 0, -1);

    vi in(n), out(n), tour(n);
    int idx = 0;
    {
        auto dfs = [&](auto &&f, int x, int p) -> void {
            tour[idx] = x;
            in[x] = idx++;
            out[x] = in[x] + sub[x];
            fore(e, g[x]) if(e != p) f(f, e, x);
        };
        dfs(dfs, 0, -1);
    }

    ll ans = count(all(a), 1);
    BIT bit(n * 2 + 1);

    int car = n;

    vi tmp;
    // rep(i, n) cout << v[i] << " ";
    // cout << endl;

    auto add = [&](int i) {
        bit.add(i + car, 1);
        tmp.eb(i + car);
    };
    auto dfs2 = [&](auto &&f, int x, int p, bool clear) -> void {
        rep(i, 1, si(g[x])) {
            int e = g[x][i];
            if(e == p) continue;
            f(f, e, x, true);
        }
        if(g[x][0] != p) f(f, g[x][0], x, false);

        ll tans = 0;
        tans += bit.sum(v[x] - a[x] + car + 1, 2 * n + 1);
        add(v[x]);
        rep(i, 1, si(g[x])) {
            int e = g[x][i];
            if(e == p) continue;
            rep(j, in[e], out[e]) {
                int t = tour[j];
                int tar = 2 * v[x] - a[x] - v[t];
                tans += bit.sum(tar + car + 1, 2 * n + 1);
            }
            rep(j, in[e], out[e]) add(v[tour[j]]);
        }
        // cout << x << ": " << tans << endl;

        ans += tans;
        if(clear) {
            fore(i, tmp) bit.add(i, -1);
            tmp.clear();
        }
    };
    dfs2(dfs2, 0, -1, true);
    cout << ans << endl;
}
0