結果

問題 No.439 チワワのなる木
ユーザー mayoko_mayoko_
提出日時 2016-04-28 22:18:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 5,000 ms
コード長 2,186 bytes
コンパイル時間 956 ms
コンパイル使用メモリ 110,036 KB
実行使用メモリ 19,684 KB
最終ジャッジ日時 2024-04-19 07:35:11
合計ジャッジ時間 2,328 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 43 ms
8,320 KB
testcase_19 AC 36 ms
8,192 KB
testcase_20 AC 56 ms
9,984 KB
testcase_21 AC 14 ms
5,376 KB
testcase_22 AC 14 ms
5,376 KB
testcase_23 AC 59 ms
11,264 KB
testcase_24 AC 68 ms
18,176 KB
testcase_25 AC 40 ms
10,752 KB
testcase_26 AC 39 ms
10,772 KB
testcase_27 AC 42 ms
19,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 100010;
int N;
string s;
vector<vi> G;

pll P[MAXN];

// v 以下の部分木にある (ww, w) を求める
pll dfs(int v, int p) {
    pll& ret = P[v];
    if (s[v] == 'w') ret.second++;
    for (int ch : G[v]) if (ch != p) {
        pll tmp = dfs(ch, v);
        ret.first += tmp.first;
        ret.second += tmp.second;
        if (s[v] == 'w') ret.first += tmp.second;
    }
    return ret;
}

ll ans;

void dfs2(int v, int par, pll p) {
    if (s[v] == 'c') {
        // c -> par 側
        if (s[v] ) ans += p.first;
        // c -> 子
        for (int ch : G[v]) if (ch != par) {
            ans += P[ch].first;
        }
    }
    // 子に移動
    for (int ch : G[v]) if (ch != par) {
        pll tmp = p;
        tmp.first += P[v].first, tmp.second += P[v].second;
        tmp.first -= P[ch].first, tmp.second -= P[ch].second;
        if (s[v] == 'w') {
            tmp.first -= P[ch].second;
            tmp.first += p.second;
        }
        dfs2(ch, v, tmp);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    cin >> s;
    for (int i = 0; i < N; i++) assert(s[i] == 'c' || s[i] == 'w');

    G.resize(N);
    for (int i = 0; i < N-1; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    dfs(0, -1);
//    for (int i = 0; i < N; i++) {
//        cout << i << "  " << P[i].first << "  " << P[i].second << endl;
//    }
    dfs2(0, -1, pll());
    cout << ans << endl;
    return 0;
}
0