結果

問題 No.439 チワワのなる木
ユーザー kimiyukikimiyuki
提出日時 2016-11-01 18:24:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 5,000 ms
コード長 1,322 bytes
コンパイル時間 983 ms
コンパイル使用メモリ 83,632 KB
実行使用メモリ 25,728 KB
最終ジャッジ日時 2023-08-16 12:21:27
合計ジャッジ時間 3,536 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 77 ms
7,236 KB
testcase_19 AC 69 ms
7,024 KB
testcase_20 AC 108 ms
8,320 KB
testcase_21 AC 26 ms
4,868 KB
testcase_22 AC 24 ms
4,416 KB
testcase_23 AC 123 ms
10,440 KB
testcase_24 AC 125 ms
23,084 KB
testcase_25 AC 91 ms
9,776 KB
testcase_26 AC 81 ms
10,204 KB
testcase_27 AC 92 ms
25,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
typedef long long ll;
using namespace std;
int main() {
    int n; string s; cin >> n >> s;
    vector<vector<int> > g(n);
    repeat (i,n-1) {
        int a, b; cin >> a >> b; -- a; -- b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int total_c = whole(count, s, 'c');
    int total_w = whole(count, s, 'w');
    ll ans = 0;
    function<pair<int, int> (int, int)> go = [&](int i, int parent) {
        vector<int> cs, ws;
        for (int j : g[i]) if (j != parent) {
            int c, w; tie(c, w) = go(j, i);
            cs.push_back(c);
            ws.push_back(w);
        }
        int acc_c = whole(accumulate, cs, 0) + (s[i] == 'c');
        int acc_w = whole(accumulate, ws, 0) + (s[i] == 'w');
        if (s[i] == 'w') {
            cs.push_back(total_c - acc_c);
            ws.push_back(total_w - acc_w);
            repeat (j, cs.size()) {
                ans += cs[j] *(ll) (total_w-1 - ws[j]);
            }
        }
        return make_pair(acc_c, acc_w);
    };
    go(0, -1);
    cout << ans << endl;
    return 0;
}
0