結果

問題 No.439 チワワのなる木
ユーザー mio_hmio_h
提出日時 2016-10-29 16:21:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 65 ms / 5,000 ms
コード長 1,453 bytes
コンパイル時間 1,821 ms
コンパイル使用メモリ 152,100 KB
実行使用メモリ 16,488 KB
最終ジャッジ日時 2023-08-16 06:45:04
合計ジャッジ時間 3,474 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 38 ms
6,988 KB
testcase_19 AC 32 ms
6,956 KB
testcase_20 AC 51 ms
8,336 KB
testcase_21 AC 12 ms
4,912 KB
testcase_22 AC 11 ms
4,392 KB
testcase_23 AC 56 ms
9,356 KB
testcase_24 AC 65 ms
15,164 KB
testcase_25 AC 40 ms
9,068 KB
testcase_26 AC 39 ms
9,136 KB
testcase_27 AC 38 ms
16,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class in{struct myIterator{int it;const bool rev;explicit constexpr myIterator(int it_, bool rev=false):it(it_),rev(rev){}int operator*(){return it;}bool operator!=(myIterator& r){return it!=r.it;}void operator++(){rev?--it:++it;}};const myIterator i,n;public:explicit constexpr in(int n):i(0),n(n){}explicit constexpr in(int i,int n):i(i,n<i),n(n){}const myIterator& begin(){return i;}const myIterator& end(){return n;}};

#include <bits/stdc++.h>
using namespace std;
// input
int n;
string s;
vector<vector<int>> adj;
// end input
using i64 = long long;
i64 cSum, wSum, ans;
pair<i64/*c*/, i64/*w*/> dfs(int cur, int par) {
    i64 partC = 0LL, partW = 0LL;
    bool is_w = s[cur] == 'w';
    for(int nv : adj[cur]) {
        if(nv == par) continue;
        i64 c, w;
        tie(c, w) = dfs(nv, cur);
        if(is_w)
          ans += c * (wSum - w - 1LL);
        partC += c;
        partW += w;
    }
    if(is_w)
      ans += partW * (cSum - partC);
    if(is_w) partW++; else partC++;
    return make_pair(partC, partW);
}
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> n;
    cin >> s;
    adj.resize(n);
    for(int _ : in(n - 1)) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        adj[a].emplace_back(b);
        adj[b].emplace_back(a);
    }
    ans = cSum = wSum = 0LL;
    for(char c : s) {
        if(c == 'c') ++cSum;
        else ++wSum;
    }
    dfs(0, -1);
    cout << ans << endl;
}
0