結果

問題 No.2892 Lime and Karin
ユーザー NachiaNachia
提出日時 2024-09-13 22:49:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 155 ms / 8,000 ms
コード長 3,531 bytes
コンパイル時間 1,152 ms
コンパイル使用メモリ 89,064 KB
実行使用メモリ 13,684 KB
最終ジャッジ日時 2024-09-13 22:49:58
合計ジャッジ時間 6,920 ms
ジャッジサーバーID
(参考情報)
judge6 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 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 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 59 ms
8,168 KB
testcase_15 AC 55 ms
7,776 KB
testcase_16 AC 94 ms
10,756 KB
testcase_17 AC 23 ms
6,944 KB
testcase_18 AC 93 ms
10,900 KB
testcase_19 AC 134 ms
12,736 KB
testcase_20 AC 8 ms
6,940 KB
testcase_21 AC 68 ms
8,996 KB
testcase_22 AC 92 ms
10,480 KB
testcase_23 AC 36 ms
6,944 KB
testcase_24 AC 128 ms
13,176 KB
testcase_25 AC 137 ms
13,236 KB
testcase_26 AC 139 ms
13,052 KB
testcase_27 AC 140 ms
12,960 KB
testcase_28 AC 130 ms
13,304 KB
testcase_29 AC 140 ms
12,776 KB
testcase_30 AC 143 ms
13,428 KB
testcase_31 AC 128 ms
13,176 KB
testcase_32 AC 146 ms
13,176 KB
testcase_33 AC 138 ms
13,684 KB
testcase_34 AC 50 ms
11,868 KB
testcase_35 AC 49 ms
11,872 KB
testcase_36 AC 50 ms
12,004 KB
testcase_37 AC 50 ms
11,876 KB
testcase_38 AC 49 ms
11,872 KB
testcase_39 AC 155 ms
13,400 KB
testcase_40 AC 146 ms
13,272 KB
testcase_41 AC 155 ms
13,272 KB
testcase_42 AC 148 ms
13,396 KB
testcase_43 AC 146 ms
13,268 KB
testcase_44 AC 45 ms
7,172 KB
testcase_45 AC 109 ms
12,392 KB
testcase_46 AC 53 ms
7,856 KB
testcase_47 AC 71 ms
9,096 KB
testcase_48 AC 30 ms
6,940 KB
testcase_49 AC 85 ms
10,432 KB
testcase_50 AC 88 ms
13,484 KB
testcase_51 AC 87 ms
13,612 KB
testcase_52 AC 88 ms
13,352 KB
testcase_53 AC 98 ms
13,356 KB
testcase_54 AC 87 ms
13,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
#include <atcoder/modint>
using Modint = atcoder::static_modint<998244353>;
using namespace std;

void testcase(){
    int N; cin >> N;
    vector<vector<int>> adj(N);
    rep(i,N-1){
        int u,v; cin >> u >> v; u--; v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    vector<int> aw(N); {
        string s; cin >> s;
        rep(i,N) aw[i] = (s[i] == '1' ? 1 : -1);
    }
    vector<int> parent(N, -1);
    vector<int> Z(N, 1);
    {
        vector<int> bfs;
        bfs.push_back(0);
        rep(i,N){
            int v = bfs[i];
            for(int w : adj[v]) if(parent[v] != w){
                parent[w] = v;
                bfs.push_back(w);
            }
        }
        for(int i=N-1; i>=1; i--) Z[parent[bfs[i]]] += Z[bfs[i]];
    }
    //cout << "##" << endl;
    i64 ans = 0;
    auto flushTable = [&](int off, const vector<int>& c, int root) -> i64 {
        auto cs = vector<int>(c.size()+1);
        rep(i,c.size()) cs[i+1] += cs[i] + c[i];
        i64 cc = i64(cs.back()) * i64(cs.back());
        for(i64 i=0; i<i64(c.size()); i++){
            i64 j = off - (i + root - off);
            if(j >= 0) cc -= i64(c[i]) * i64(cs[min<i64>(cs.size() - 1, j + 1)]);
        }
        return cc;
    };
    //for(auto p : parent) cout << p << " ";
    //cout << endl;
    //for(auto p : Z) cout << p << " ";
    //cout << endl;
    auto cd = [&](auto& cd, int v) -> void {
        while(true){
            int nx = -1;
            for(int w : adj[v]) if(Z[w] * 2 > Z[v]){
                Z[v] -= Z[w]; Z[w] += Z[v];
                parent[v] = w; parent[w] = -1;
                nx = w;
                break;
            }
            if(nx >= 0) v = nx; else break;
        }
        //cout << "v = " << v << endl;
        int off = Z[v];
        vector<int> cnt(Z[v] * 2 + 1);
        cnt[off] += 1;
        int rootaw = aw[v];
        Z[v] = 0;
        for(int w : adj[v]) if(Z[w] != 0){
            //cout << "w = " << w << endl;
            parent[w] = -1;
            int offw = Z[w];
            vector<int> cntw(Z[w] * 2 + 1);
            vector<pair<int, int>> bfs;
            bfs.push_back({ w, 0 });
            rep(q,bfs.size()){
                auto [x,c] = bfs[q];
                //cout << "x = " << x << " , c = " << c << endl;
                c += aw[x];
                cntw[c + offw] += 1;
                cnt[c + off] += 1;
                for(int y : adj[x]) if(Z[y] != 0 && parent[x] != y){
                    bfs.push_back({ y, c });
                }
            }
            //cout << "v = " << v << " , w = " << w << endl;
            //for(auto a : cntw) cout << a << " ";
            //cout << endl;
            ans -= flushTable(offw, cntw, aw[v]);
            cd(cd, w);
        }
           // cout << "v = " << v << endl;
           // for(auto a : cnt) cout << a << " ";
           // cout << endl;
        ans += flushTable(off, cnt, aw[v]);
    };
    rep(i,N) if(aw[i] > 0) ans += 1;
    cd(cd, 0);
    ans /= 2;
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    testcase();
    return 0;
}
0