結果

問題 No.2892 Lime and Karin
ユーザー 👑 AngrySadEightAngrySadEight
提出日時 2024-08-21 17:54:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,742 bytes
コンパイル時間 6,294 ms
コンパイル使用メモリ 247,164 KB
実行使用メモリ 30,228 KB
最終ジャッジ日時 2024-08-21 17:55:21
合計ジャッジ時間 22,574 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,940 KB
testcase_06 WA -
testcase_07 AC 2 ms
6,944 KB
testcase_08 WA -
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 144 ms
28,464 KB
testcase_35 AC 154 ms
28,468 KB
testcase_36 AC 153 ms
28,596 KB
testcase_37 AC 146 ms
28,592 KB
testcase_38 AC 147 ms
28,596 KB
testcase_39 AC 1,305 ms
28,600 KB
testcase_40 AC 1,278 ms
30,228 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 130 ms
10,984 KB
testcase_45 AC 326 ms
20,532 KB
testcase_46 AC 180 ms
12,392 KB
testcase_47 AC 223 ms
15,084 KB
testcase_48 AC 93 ms
8,704 KB
testcase_49 AC 259 ms
16,660 KB
testcase_50 AC 257 ms
25,484 KB
testcase_51 AC 272 ms
25,492 KB
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
権限があれば一括ダウンロードができます

ソースコード

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();
    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++;
                    }
                }
            }
        }
        if (vecs.size() == 0) {
            continue;
        }
        // debug(vecs);
        // debug(minus_mins);
        // cout << endl;

        ll minus_min = 1;
        ll plus_max = -1;
        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);
        ll dline = -1;
        if (S[root] == '0') {
            dline = 1;
        }
        for (ll j = max(dline + 1, -minus_min * 2 + dline + 1);
             j < sq_all.size(); j++) {
            ans += sq_all[j] / 2;
        }
    }
    cout << ans << endl;
}
0