結果

問題 No.2892 Lime and Karin
ユーザー 👑 AngrySadEightAngrySadEight
提出日時 2024-08-30 17:06:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,444 ms / 8,000 ms
コード長 9,037 bytes
コンパイル時間 7,697 ms
コンパイル使用メモリ 246,480 KB
実行使用メモリ 30,356 KB
最終ジャッジ日時 2024-08-30 17:07:06
合計ジャッジ時間 24,368 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 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,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 166 ms
12,920 KB
testcase_15 AC 154 ms
12,412 KB
testcase_16 AC 294 ms
17,892 KB
testcase_17 AC 62 ms
7,424 KB
testcase_18 AC 266 ms
17,792 KB
testcase_19 AC 389 ms
22,732 KB
testcase_20 AC 21 ms
6,944 KB
testcase_21 AC 194 ms
14,408 KB
testcase_22 AC 286 ms
17,548 KB
testcase_23 AC 105 ms
9,472 KB
testcase_24 AC 386 ms
22,976 KB
testcase_25 AC 388 ms
22,772 KB
testcase_26 AC 414 ms
23,252 KB
testcase_27 AC 384 ms
23,160 KB
testcase_28 AC 374 ms
22,940 KB
testcase_29 AC 407 ms
23,048 KB
testcase_30 AC 379 ms
22,904 KB
testcase_31 AC 372 ms
22,900 KB
testcase_32 AC 404 ms
23,156 KB
testcase_33 AC 372 ms
22,780 KB
testcase_34 AC 147 ms
28,596 KB
testcase_35 AC 147 ms
28,464 KB
testcase_36 AC 148 ms
28,464 KB
testcase_37 AC 150 ms
28,460 KB
testcase_38 AC 157 ms
28,492 KB
testcase_39 AC 1,356 ms
28,600 KB
testcase_40 AC 1,444 ms
30,356 KB
testcase_41 AC 419 ms
24,140 KB
testcase_42 AC 494 ms
25,712 KB
testcase_43 AC 529 ms
24,756 KB
testcase_44 AC 146 ms
11,112 KB
testcase_45 AC 397 ms
20,536 KB
testcase_46 AC 166 ms
12,336 KB
testcase_47 AC 225 ms
15,084 KB
testcase_48 AC 100 ms
8,832 KB
testcase_49 AC 278 ms
16,796 KB
testcase_50 AC 265 ms
25,488 KB
testcase_51 AC 272 ms
25,620 KB
testcase_52 AC 255 ms
25,496 KB
testcase_53 AC 280 ms
25,504 KB
testcase_54 AC 258 ms
25,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <atcoder/all>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using namespace atcoder;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define mod1 1000000007
#define mod2 998244353
#define mod3 100000007
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vvl vector<vector<ll>>
#define vvb vector<vector<bool>>
#define vvvi vector<vector<vector<int>>>
#define vvvl vector<vector<vector<ll>>>
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define vvpii vector<vector<pair<int, int>>>
#define vvpll vector<vector<pair<ll, ll>>>

using mint = modint998244353;

template <typename T>
void debug(T e) {
    cerr << e << endl;
}

template <typename T>
void debug(vector<T> &v) {
    rep(i, v.size()) { cerr << v[i] << " "; }
    cerr << endl;
}

template <typename T>
void debug(vector<vector<T>> &v) {
    rep(i, v.size()) {
        rep(j, v[i].size()) { cerr << v[i][j] << " "; }
        cerr << endl;
    }
}

template <typename T>
void debug(vector<pair<T, T>> &v) {
    rep(i, v.size()) { cerr << v[i].first << " " << v[i].second << endl; }
}

template <typename T>
void debug(set<T> &st) {
    for (auto itr = st.begin(); itr != st.end(); itr++) {
        cerr << *itr << " ";
    }
    cerr << endl;
}

template <typename T>
void debug(multiset<T> &ms) {
    for (auto itr = ms.begin(); itr != ms.end(); itr++) {
        cerr << *itr << " ";
    }
    cerr << endl;
}

template <typename T>
void debug(map<T, T> &mp) {
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
        cerr << itr->first << " " << itr->second << endl;
    }
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << H << " ";
    debug_out(T...);
}

struct centroid_decomposition {
    vector<vector<ll>> graph;
    vector<ll> subtreesize;
    vector<bool> isremoved;
    vector<ll> parent;
    vector<ll> centroids;

    centroid_decomposition(vector<ll> &U, vector<ll> &V, ll N) {
        graph.resize(N);
        for (ll i = 0; i < N - 1; i++) {
            graph[U[i]].push_back(V[i]);
            graph[V[i]].push_back(U[i]);
        }
        subtreesize.assign(N, 0);
        isremoved.assign(N, false);
        parent.resize(N);
    }

    void calc_size(ll v, ll p) {
        subtreesize[v] = 1;
        for (ll x : graph[v]) {
            if (x == p) {
                continue;
            }
            if (isremoved[x]) {
                continue;
            }
            calc_size(x, v);
            subtreesize[v] += subtreesize[x];
        }
    }

    void find(ll v, ll p) {
        calc_size(v, p);
        ll size = subtreesize[v];
        ll pp = -1;
        ll now = v;
        while (true) {
            bool cont = false;
            for (ll x : graph[now]) {
                if (!isremoved[x] && x != pp && subtreesize[x] > size / 2) {
                    cont = true;
                    pp = now;
                    now = x;
                    cont = true;
                    break;
                }
            }
            if (!cont) {
                break;
            }
        }
        parent[now] = p;
        isremoved[now] = true;
        centroids.push_back(now);
        for (ll x : graph[now]) {
            if (!isremoved[x]) {
                find(x, now);
            }
        }
    }

    vector<ll> make_centroids() {
        find(0, -1);
        return centroids;
    }

    vector<ll> print_parent() { return parent; }
};

int main() {
    ll N;
    cin >> N;
    vector<ll> u(N - 1);
    vector<ll> v(N - 1);
    for (ll i = 0; i < N - 1; i++) {
        cin >> u[i] >> v[i];
        u[i]--;
        v[i]--;
    }
    string S;
    cin >> S;
    centroid_decomposition CD(u, v, N);
    vector<ll> centroids = CD.make_centroids();
    // debug(centroids);
    vector<vector<ll>> graph(N, vector<ll>(0));
    for (ll i = 0; i < N - 1; i++) {
        graph[u[i]].push_back(v[i]);
        graph[v[i]].push_back(u[i]);
    }

    vector<bool> used(N, false);
    vector<ll> dist(N, -1);
    vector<ll> prev(N, -1);
    ll ans = 0;
    for (ll i = 0; i < N; i++) {
        ll root = centroids[i];
        used[root] = true;

        vector<vector<ll>> vecs(0, vector<ll>(0));
        vector<ll> minus_mins(0);

        queue<ll> que;
        for (ll j = 0; j < graph[root].size(); j++) {
            if (used[graph[root][j]]) {
                continue;
            }
            vector<ll> vecs_sub(0);
            ll minus_sub = 1;
            if (S[graph[root][j]] == '1') {
                dist[graph[root][j]] = 1;
            } else {
                dist[graph[root][j]] = -1;
            }
            vecs_sub.push_back(dist[graph[root][j]]);
            minus_sub = min(minus_sub, dist[graph[root][j]]);
            que.push(graph[root][j]);
            prev[graph[root][j]] = root;
            while (que.size()) {
                ll p = que.front();
                que.pop();
                for (ll x : graph[p]) {
                    if (used[x]) {
                        continue;
                    }
                    if (x == prev[p]) {
                        continue;
                    }
                    if (S[x] == '1') {
                        dist[x] = dist[p] + 1;
                    } else {
                        dist[x] = dist[p] - 1;
                    }
                    minus_sub = min(minus_sub, dist[x]);
                    vecs_sub.push_back(dist[x]);
                    que.push(x);
                    prev[x] = p;
                }
            }
            vecs.push_back(vecs_sub);
            minus_mins.push_back(minus_sub);
        }
        // 定数部分の答えも探す
        if (S[root] == '1') {
            ans++;
        }
        for (ll j = 0; j < vecs.size(); j++) {
            for (ll k = 0; k < vecs[j].size(); k++) {
                if (S[root] == '1') {
                    if (vecs[j][k] >= 0) {
                        ans++;
                    }
                } else {
                    if (vecs[j][k] >= 2) {
                        ans++;
                    }
                }
            }
        }
        // debug(vecs);
        if (vecs.size() == 0) {
            continue;
        }
        // debug(vecs);
        // debug(minus_mins);
        // cout << endl;

        ll minus_min = 2;
        ll plus_max = -2;
        for (ll j = 0; j < vecs.size(); j++) {
            for (ll k = 0; k < vecs[j].size(); k++) {
                minus_min = min(minus_min, vecs[j][k]);
                plus_max = max(plus_max, vecs[j][k]);
            }
        }
        // debug_out(plus_max, minus_min);
        vector<ll> all_vec(plus_max - minus_min + 1, 0);
        for (ll j = 0; j < vecs.size(); j++) {
            for (ll k = 0; k < vecs[j].size(); k++) {
                all_vec[vecs[j][k] - minus_min]++;
            }
        }
        // debug(all_vec);
        vector<ll> sq_all = convolution_ll(all_vec, all_vec);
        // debug(sq_all);
        for (ll j = 0; j < vecs.size(); j++) {
            ll plus_sub = -1;
            ll minus_sub = 1;
            for (ll k = 0; k < vecs[j].size(); k++) {
                plus_sub = max(plus_sub, vecs[j][k]);
                minus_sub = min(minus_sub, vecs[j][k]);
            }
            vector<ll> sub_vec(plus_sub - minus_sub + 1);
            for (ll k = 0; k < vecs[j].size(); k++) {
                sub_vec[vecs[j][k] - minus_sub]++;
            }
            vector<ll> sq_sub = convolution_ll(sub_vec, sub_vec);
            for (ll k = 0; k < sq_sub.size(); k++) {
                sq_all[(minus_sub - minus_min) * 2 + k] -= sq_sub[k];
            }
        }
        // debug(sq_all);
        // debug(minus_min);
        ll dline = -1;
        if (S[root] == '0') {
            dline = 1;
        }
        if (-minus_min * 2 + dline + 1 >= 0) {
            for (ll j = -minus_min * 2 + dline + 1; j < sq_all.size(); j++) {
                ans += sq_all[j] / 2;
            }
        } else {
            for (ll j = 0; j < sq_all.size(); j++) {
                ans += sq_all[j] / 2;
            }
        }

        // cout << ans << endl;
    }
    // cout << endl;
    cout << ans << endl;
}
0