結果

問題 No.439 チワワのなる木
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-29 01:19:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 5,000 ms
コード長 2,661 bytes
コンパイル時間 1,574 ms
コンパイル使用メモリ 171,896 KB
実行使用メモリ 18,100 KB
最終ジャッジ日時 2023-08-01 08:04:53
合計ジャッジ時間 3,206 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,868 KB
testcase_01 AC 2 ms
4,924 KB
testcase_02 AC 3 ms
4,928 KB
testcase_03 AC 2 ms
4,872 KB
testcase_04 AC 2 ms
4,856 KB
testcase_05 AC 2 ms
5,132 KB
testcase_06 AC 2 ms
5,124 KB
testcase_07 AC 2 ms
4,860 KB
testcase_08 AC 3 ms
4,964 KB
testcase_09 AC 3 ms
4,944 KB
testcase_10 AC 3 ms
4,880 KB
testcase_11 AC 3 ms
4,996 KB
testcase_12 AC 3 ms
5,096 KB
testcase_13 AC 3 ms
4,892 KB
testcase_14 AC 3 ms
4,892 KB
testcase_15 AC 3 ms
5,012 KB
testcase_16 AC 3 ms
4,968 KB
testcase_17 AC 3 ms
5,088 KB
testcase_18 AC 40 ms
9,108 KB
testcase_19 AC 35 ms
8,852 KB
testcase_20 AC 54 ms
10,436 KB
testcase_21 AC 15 ms
6,508 KB
testcase_22 AC 14 ms
6,220 KB
testcase_23 AC 60 ms
11,528 KB
testcase_24 AC 64 ms
16,784 KB
testcase_25 AC 39 ms
11,148 KB
testcase_26 AC 36 ms
11,008 KB
testcase_27 AC 41 ms
18,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;

// O(N)解
// 2回DFSする

struct Chiwawa {
    int w, ww;
    Chiwawa() { w = 0, ww = 0; }
};

const int MAX_N = 100000;

int N;
string s;
vector<vector<int>> G;
Chiwawa cww[MAX_N];    // 各頂点を根とした部分木に含まれる(w, ww)の個数 (一番上の親は0)

// 配列cwwを用意するためのdfs
Chiwawa dfs(int now, int pre) {
    Chiwawa ret;

    for (auto nxt : G[now]) {
        if (nxt == pre) continue;
        auto child = dfs(nxt, now);
        ret.w += child.w;
        ret.ww += child.ww;
    }

    if (s[now] == 'w') {
        // 自分がwなら、子のwの数だけwwが増える
        ret.ww += ret.w;
        // wは1増える
        ++ret.w;
    }

    cww[now] = ret;
    return ret;
}

int ans; // 答えの記録用

// 答えを求めるdfs
// p: vの部分木に含まれない(w, ww)の個数
void dfs2(int v, int pre, Chiwawa p) {
    // この頂点がcなら、つなげられるwwの個数を答えに加える
    if (s[v] == 'c') {
        ans += p.ww;    // pのwwとつなげたとき
        for (auto ch : G[v]) {
            if (ch == pre) continue;
            ans += cww[ch].ww;  // chの部分木のwwとつなげたとき
        }
    }


    for (auto ch : G[v]) {
        if (ch == pre) continue;
        // 次の子に渡すためにpを更新する
        auto tmp = p;
        // vの部分木のうち、chの部分木の分を除く
        tmp.w += cww[v].w, tmp.ww += cww[v].ww;
        tmp.w -= cww[ch].w, tmp.ww -= cww[ch].ww;
        if (s[v] == 'w') {
            // vが 'w' の場合は、 vの 'w' を使っている分のwwの個数が変化する
            // v の 'w' と chの部分木の 'w' を使っている ww は減る
            tmp.ww -= cww[ch].w;
            // v の 'w' と p の 'w' を使っている ww が増える
            tmp.ww += p.w;
        }
        // 子に移動
        dfs2(ch, v, tmp);
    }


}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    cin >> N;
    cin >> s;

    G.resize(N);
    rep(i, N - 1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }

    // 準備
    dfs(0, -1);

    // 本番
    dfs2(0, -1, Chiwawa());

    cout << ans << endl;

}
0