結果

問題 No.439 チワワのなる木
ユーザー tottoripapertottoripaper
提出日時 2016-10-29 01:09:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,084 bytes
コンパイル時間 4,469 ms
コンパイル使用メモリ 149,876 KB
実行使用メモリ 18,948 KB
最終ジャッジ日時 2023-08-15 22:06:31
合計ジャッジ時間 2,964 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,972 KB
testcase_01 AC 3 ms
5,908 KB
testcase_02 AC 3 ms
5,976 KB
testcase_03 AC 3 ms
5,908 KB
testcase_04 AC 3 ms
5,892 KB
testcase_05 AC 4 ms
5,908 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 3 ms
5,836 KB
testcase_12 WA -
testcase_13 WA -
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 AC 94 ms
17,240 KB
testcase_25 AC 43 ms
9,720 KB
testcase_26 WA -
testcase_27 AC 43 ms
18,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int N;
string nodes;
vector<int> G[100000];

int c_cnts[100000];

ll rec(int u, int p, int c_cnt, int w_cnt, ll cw_cnt){
    ll res = 0ll;

    if(nodes[u] == 'c'){
        res = 1ll * w_cnt * (w_cnt - 1) / 2;
    }else{
        // res = cw_cnt;
    }

    // printf("at %d, get %lld as bonus\n", u, res);

    for(auto v : G[u]){
        if(v == p){continue;}

        if(nodes[u] == 'c'){
            res += rec(v, u, c_cnt + 1, w_cnt, cw_cnt);
        }else{
            res += rec(v, u, c_cnt, w_cnt + 1, cw_cnt + c_cnt);
        }
    }

    return res;
}

int dfs(int u, int p){
    int res = nodes[u] == 'c';

    for(int v : G[u]){
        if(v == p){continue;}

        res += dfs(v, u);
    }

    return c_cnts[u] = res;
}

tuple<ll,ll> rec2(int u, int p){
    ll res = 0ll, all_w_cnt = 0;
    
    for(auto v : G[u]){
        if(v == p){continue;}

        ll child_res, w_cnt;
        
        tie(child_res, w_cnt) = rec2(v, u);

        res += child_res;

        if(nodes[u] == 'w'){
            // printf("added %lld at %d ; c: %d, w_cnt: %lld\n", 1ll * (c_cnts[0] - c_cnts[v]) * w_cnt, u, c_cnts[0] - c_cnts[v], w_cnt);
            // printf("n, w_cnt: %d, %lld (%d)\n", n, w_cnt, u);
            res += 1ll * (c_cnts[0] - c_cnts[v]) * w_cnt;
        }
        
        all_w_cnt += w_cnt;
    }

    if(nodes[u] == 'w'){++all_w_cnt;}

    return std::make_tuple(res, all_w_cnt);
}

int main(){
    //*
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    /*/
      /*/

    std::cin >> N;
    std::cin >> nodes;

    for(int i=0;i<N-1;++i){
        int a, b;
        std::cin >> a >> b;

        --a; --b;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }

    dfs(0, -1);
    ll res = rec(0, -1, 0, 0, 0) + fst(rec2(0, -1));
    printf("%lld\n", res);
}
0